Order of tuples in dict.items()

Will McGugan will at willNOmcguganSPAM.com
Sun Oct 14 16:28:32 CEST 2007


If I have two dictionaries containing identical values, can I be sure 
that the items() method will return tuples in the same order?

I tried an experiment with CPython and it does appear to be the case.

 >>> a=dict(a=1, b=1, c=2)
 >>> b=dict(c=2, a=1, b=1)
 >>> a
{'a': 1, 'c': 2, 'b': 1}
 >>> b
{'a': 1, 'c': 2, 'b': 1}
 >>> a.items()
[('a', 1), ('c', 2), ('b', 1)]
 >>> b.items()
[('a', 1), ('c', 2), ('b', 1)]

Can I rely on this behavior?


Will McGugan

blog: http://www.willmcgugan.com

More information about the Python-list mailing list