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


Question

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              
1
276
3/29/2013 7:53:36 AM

Accepted Answer

You need to use something like 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.

EDIT
Since Python 3 iteritems() is no longer supported. Use items() instead.

for field, possible_values in fields.items():
    print(field, possible_values)
411
6/26/2019 8:38:44 AM

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon