You can use the enumerate() function:
for count, (key, value) in enumerate(my_dict.iteritems(), 1): print key, value, count
enumerate() effectively adds a counter to the iterator that you are looping. In the above example, I will tell enumerate() to start counting from 1 according to your example; the default value should start at 0.
Demo:
>>> somedict = {'foo': 'bar', 42: 'Life, the Universe and Everything', 'monty': 'python'} >>> for count, (key, value) in enumerate(somedict.iteritems(), 1): ... print key, value, count ... 42 Life, the Universe and Everything 1 foo bar 2 monty python 3
source share