'too many values to unpack', iterating over a dict. key=>string, value=>list
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Meditation
--
Chapters
00:00 Question
00:28 Accepted answer (Score 574)
01:07 Answer 2 (Score 88)
01:23 Answer 3 (Score 39)
01:55 Answer 4 (Score 13)
02:12 Thank you
--
Full question
https://stackoverflow.com/questions/5466...
Accepted answer links:
[items()]: http://docs.python.org/library/stdtypes....
[iteritems()]: http://docs.python.org/library/stdtypes....
[this answer]: https://stackoverflow.com/a/3294899/1489...
[iteritems()]: https://docs.python.org/3/whatsnew/3.0.h...
Answer 2 links:
[items]: https://docs.python.org/3.4/library/stdt...
Answer 3 links:
[iteritems]: http://docs.python.org/library/stdtypes....
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Meditation
--
Chapters
00:00 Question
00:28 Accepted answer (Score 574)
01:07 Answer 2 (Score 88)
01:23 Answer 3 (Score 39)
01:55 Answer 4 (Score 13)
02:12 Thank you
--
Full question
https://stackoverflow.com/questions/5466...
Accepted answer links:
[items()]: http://docs.python.org/library/stdtypes....
[iteritems()]: http://docs.python.org/library/stdtypes....
[this answer]: https://stackoverflow.com/a/3294899/1489...
[iteritems()]: https://docs.python.org/3/whatsnew/3.0.h...
Answer 2 links:
[items]: https://docs.python.org/3.4/library/stdt...
Answer 3 links:
[iteritems]: http://docs.python.org/library/stdtypes....
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
ACCEPTED ANSWER
Score 602
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.
ANSWER 2
Score 89
For Python 3.x iteritems has been removed. Use items instead.
for field, possible_values in fields.items():
print(field, possible_values)
ANSWER 3
Score 40
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
ANSWER 4
Score 14
For lists, use enumerate
for field, possible_values in enumerate(fields):
print(field, possible_values)
iteritems will not work for list objects