'too many values to unpack', iterating over a dict. key=>string, value=>list

Python

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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

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

Solution 3 - Python

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

Solution 4 - Python

For lists, use enumerate

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

iteritems will not work for list objects

Solution 5 - Python

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]

Solution 6 - Python

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)

Solution 7 - Python

In Python3 iteritems() is no longer supported

Use .items

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

Solution 8 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontipuView Question on Stackoverflow
Solution 1 - PythonPhilip SouthamView Answer on Stackoverflow
Solution 2 - PythonMeistroView Answer on Stackoverflow
Solution 3 - PythonMike LewisView Answer on Stackoverflow
Solution 4 - PythonJordanView Answer on Stackoverflow
Solution 5 - PythonVaibhav DesaiView Answer on Stackoverflow
Solution 6 - Pythonsatya narayan pardhiView Answer on Stackoverflow
Solution 7 - Pythonishandutta2007View Answer on Stackoverflow
Solution 8 - PythonbenleroterView Answer on Stackoverflow