Wednesday, October 31, 2012

Python: iterate dict

dict2 = {"key1":"value1","key2":"value2","key3":"value3"}

for k in dict2.keys():
    myValue = dict2[k]
    print "key: %s , value: %s" % (k,myValue)

Result:


C:\Users\John\Desktop>python test.py
key: key3 , value: value3
key: key2 , value: value2
key: key1 , value: value1


Resource:
http://stackoverflow.com/questions/6332691/python-dictionary-iteration


No comments: