You can use the inbuilt method iteritems() or items() or simply a for loop to iterate over dictionaries using for loop in Python as shown in the below example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
mydict = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5} ## Any version for mykey in mydict: print(mykey,mydict[mykey]) ## Python version 2 for mykey, myvalue in mydict.iteritems(): print(mykey,myvalue) ## Python version 3 for mykey, myvalue in mydict.items(): print(mykey,myvalue) ## Returns ## a 1 ## b 2 ## c 3 ## d 4 ## e 5 |