Dictionary Enumeration in Python

I am trying to list through such a dictionary, but it does not work. What is the easiest way to iterate through a dictionary in python when listing each entry?

for i, k, v in enumerate(my_dict.iteritems()):
    print i, k, v
+4
source share
1 answer

You just need to add the brackets around the (k, v)tuple:

>>> d = {1: 'foo', 2: 'bar'}
>>> for i, (k, v) in enumerate(d.iteritems()):
...     print i, k, v
...
0 1 foo
1 2 bar
+10
source

Source: https://habr.com/ru/post/1659303/


All Articles