Iterating order of dict.items()

Robin Cull robin.cull at pace.co.uk
Tue Jul 22 11:06:28 EDT 2003


In a script I am writing, when I iterated over the items in a dict
using:

for x,y in mydict.items():
    doSomethingWith(x,y)

The order the items comes out in appears to be undefined.  I would
have thought that without any other sorting that the items would just
come out in the order that the dict was defined in, so:

mydict = {"one": 1, "two": 2, "three": 3, "four": 4}

would yield values: 

x     | y
------+--
one   | 1
two   | 2
three | 3 
four  | 4

However the order appears to be random say three, four, one, two for
example

So, is the order of dict.items() defined?  If so, what is that order? 
Is there any way to control the order my dict is iterated over?

Thanks a lot.  

Cheers, 

Robin




More information about the Python-list mailing list