in how many way i traverse dictionary in python???
Asked
Active
Viewed 1,613 times
-1
-
Didn't you ask this already?: http://stackoverflow.com/questions/1006575/how-do-i-traverse-a-dictionary-in-python-closed – gnovice Jun 18 '09 at 04:43
-
Also, it duplicates this question: http://stackoverflow.com/questions/380734/how-to-do-this-python-dictionary-traverse-and-search – mezoid Jun 18 '09 at 04:46
3 Answers
1
Many ways!
testdict = {"bob" : 0, "joe": 20, "kate" : 73, "sue" : 40}
for items in testdict.items():
print (items)
for key in testdict.keys():
print (key, testdict[key])
for item in testdict.iteritems():
print item
for key in testdict.iterkeys():
print (key, testdict[key])
That's a few, but that begins to departing from these simple ways into something more complex. All code was tested.

Jay Atkinson
- 3,279
- 2
- 27
- 41
0
http://docs.python.org/tutorial/datastructures.html#looping-techniques
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.iteritems():
... print k, v
...
gallahad the pure
robin the brave

David Johnstone
- 24,300
- 14
- 68
- 71