[Tutor] How to traverse a dictionary

gerrit@nl.linux.org gerrit@nl.linux.org
Mon, 23 Oct 2000 20:29:10 +0200


<quote name="Zakir Hussain" date="972288568" email="zakir@nortelnetworks.com">
> I have a dictionary with keys that are different names of sessions and each
> key contains START TIME and END TIME
> I want to go through dictionary and perform some calculations. I can't
> figure out how to go through the dictionary from the start 
> to end 
</quote>

>>> d = {"a": "first", "b": "second", "c": "third"}
>>> d.items()
[('b', 'second'), ('c', 'third'), ('a', 'first')]
>>> for key, value in d.items():
...     print "key is", key, "value is", value
...
key is b value is second
key is c value is third
key is a value is first

It isn't alfabetically because of the efficient way in which python
stores its dictionairies. Don't worry 'bout that!

regards,
Gerrit.

-- 
****************************************************************************
*  Save Mother Earth! -- Earth First! -- visit http://www.earthfirst.org/!
****************************************************************************