Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerating through a dictionary in Python [duplicate]

I am trying to enumerate through a dictionary like this but it does not work. What's the simplest way to iterate through a dictionary in python while enumerating each entry?

for i, k, v in enumerate(my_dict.iteritems()):
    print i, k, v
like image 549
user2399453 Avatar asked Dec 24 '22 00:12

user2399453


1 Answers

You just need to add parenthesis around (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
like image 154
niemmi Avatar answered Jan 22 '23 16:01

niemmi