Possible Duplicate:
python looping seems to not follow sequence?
In what order does python display dictionary keys?
d = {'x': 9, 'y': 10, 'z': 20}
for key in d:
print d[key]
The above code give different outputs every time I run it. Not exactly different outputs, but output in different sequence. I executed the code multiple times using Aptana 3.
First Execution Gave:
10
9
20
Second Execution Gave:
20
10
9
I also executed the code in an online IDE http://labs.codecademy.com. There the output was always 10 9 20
I just wanted to know why is this. Ideally it should have printed 9 10 20 every time I execute the above code. Please Explain.
A dictionary is a mapping of keys to values; it does not have an order.
You want a
collections.OrderedDict:Note, however, that dictionary ordering is deterministic — if you iterate over the same dictionary twice, you will get the same results.