Possible Duplicate:
Python dictionary, keep keys/values in same order as declared
I’m going through some exercises on code academy when I noticed the dictionaries I was creating inside my python interpreter were coming back with unexpected results.
I’m using python2.7 and when I enter:
>>> dict_a = {'x':9, 'y':10, 'z':20}
And I call it:
>>> dict_a
{'y':10, 'x':9, 'z':20}
So I put in:
>>> dict_b = {'a':1, 'b':2, 'c':3}
>>> dict_b
{'a':1, 'c':3, 'b':2}
I’m thinking I’ve missed something basic here but, after doing some searching I don’t know what? If someone could help explain why this is happening, it would be very much appreciated.
Dictionaries are unsorted. The order they display in is due to internal logic, and won’t correspond to the order you define them in. However, since you don’t access a dictionary by order but by key, this rarely matters.