403

I am getting the too many values to unpack error. Any idea how I can fix this?

first_names = ['foo', 'bar']
last_names = ['gravy', 'snowman']

fields = {
    'first_names': first_names,
    'last_name': last_names,
}        

for field, possible_values in fields:  # error happens on this line
wjandrea
  • 28,235
  • 9
  • 60
  • 81
tipu
  • 9,464
  • 15
  • 65
  • 98

8 Answers8

582

Python 3

Use items().

for field, possible_values in fields.items():
    print(field, possible_values)

Python 2

Use iteritems().

for field, possible_values in fields.iteritems():
    print field, possible_values

See this answer for more information on iterating through dictionaries, such as using items(), across Python versions.

For reference, iteritems() was removed in Python 3.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Philip Southam
  • 15,843
  • 6
  • 28
  • 20
88

For Python 3.x iteritems has been removed. Use items instead.

for field, possible_values in fields.items():
    print(field, possible_values)
arielf
  • 5,802
  • 1
  • 36
  • 48
Meistro
  • 3,664
  • 2
  • 28
  • 33
39

You want to use iteritems. This returns an iterator over the dictionary, which gives you a tuple(key, value)

>>> for field, values in fields.iteritems():
...     print field, values
... 
first_names ['foo', 'bar']
last_name ['gravy', 'snowman']

Your problem was that you were looping over fields, which returns the keys of the dictionary.

>>> for field in fields:
...     print field
... 
first_names
last_name
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
13

For lists, use enumerate

for field, possible_values in enumerate(fields):
    print(field, possible_values)

iteritems will not work for list objects

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
Jordan
  • 149
  • 1
  • 6
  • This should be a correct answer (tested) for python 2.7 at least. – Dung May 13 '19 at 17:36
  • The question is about a dictionary, not a list. Either way, those names are wrong: "`field`" is actually an index, and "`possible_values`" is actually the field. – wjandrea Mar 11 '22 at 03:18
3

you are missing fields.iteritems() in your code.

You could also do it other way, where you get values using keys in the dictionary.

for key in fields:
    value = fields[key]
Vaibhav Desai
  • 2,334
  • 2
  • 25
  • 29
-1

Can't be iterating directly in dictionary. So you can through converting into tuple.

first_names = ['foo', 'bar']
last_names = ['gravy', 'snowman']

fields = {
    'first_names': first_names,
    'last_name': last_names,
         } 
tup_field=tuple(fields.items())
for names in fields.items():
     field,possible_values = names
     tup_possible_values=tuple(possible_values)
     for pvalue in tup_possible_values:
           print (field + "is" + pvalue)
Maciej Jureczko
  • 1,560
  • 6
  • 19
  • 23
-1

In Python3 iteritems() is no longer supported

Use .items

for field, possible_values in fields.items():
    print(field, possible_values)
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
  • Please don't post duplicate answers. The [top answer](https://stackoverflow.com/a/5466625/4518341) and others already cover `dict.items()` in Python 3. – wjandrea Mar 11 '22 at 03:10
-1

Just thought I'd throw this in. I have the "too many values to unpack (expected 2)" crop up today. Infuriating but is was due to missing a comma in a choice list.

CapTextureChoices = [
("initial", ""),
("Shaggy", "Shaggy"),
("Wrinkled", "Wrinkled"),
("Striate," "Striate"),
("Downy", "Downy")
]

the missing comma between "Striate," "Striate" was the culprit

  • This is the answer to a different question. Normally I'd say post it separately, but where the cause was a typo, I wouldn't bother. Please read [ask]. – wjandrea Mar 11 '22 at 03:16