2

Possible Duplicate:
Python dictionary, keep keys/values in same order as declared

If I have in a particular program two different dictionaries with the same keys (but different values), will .keys() be in the same order? I made a few tests and it seems to be the case, but without knowing are how the internals of the dict I am not sure if this is guaranteed.

Thanks,

Community
  • 1
  • 1
Hernan
  • 5,811
  • 10
  • 51
  • 86
  • 2
    Related: [Are keys and values always in the same order](http://stackoverflow.com/questions/835092/python-dictionary-are-keys-and-values-always-the-same-order) – Brendan Long Mar 20 '12 at 19:58
  • Thanks but is not a duplicate. I am not asking, if the insertion order is kept (which is not). I am asking if given two dictionaries with the same keys (but maybe different history), the order is the same. – Hernan Mar 20 '12 at 19:59
  • 1
    @bernie - An ordered dictionary is not the same as always ordering the same way. For example, a Java HashMap is in an arbitrary order, but it will always be the same arbitrary order for a particular set of keys. – Brendan Long Mar 20 '12 at 20:00
  • I got the answer that I was looking but, the question is not an exact duplicte of "Python dictionary, keep keys/values in same order as declared". I think Brendan Long gave a good example. – Hernan Mar 20 '12 at 20:07

5 Answers5

10

You cannot rely on the key order at all:

>>> {1: None, 9: None}
{1: None, 9: None}
>>> {9: None, 1: None}
{9: None, 1: None}
>>> {1: None, 2: None}
{1: None, 2: None}
>>> {2: None, 1: None}
{1: None, 2: None}

Dictionaries are unordered. In Python 2.7, there is collections.OrderedDict, though.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Python 3.6 dictionaries have keys ordered according to insertion order ... the order in Python 2.7 will produce the same results for the same keys, but is not a defined order. – F1Rumors May 13 '19 at 15:37
  • @F1Rumors: Even in Python 2.7 the order is not consistent due to hash randomization. And in Pyton 3.6 it is still considered an implementation detail that dicts are ordered – this property was only made part of the interface of dictionaries in Python 3.7. – Sven Marnach May 13 '19 at 20:47
  • @SvenMarnach agreed, you could turn on randomization for 2.7 if you wished – F1Rumors May 14 '19 at 02:09
3

It's not guaranteed by the Python language.

The implementation of the CPython interpreter may have returned keys in the same order in some past versions, but to fix a security vulnerability this is very much not-guaranteed in current and future versions.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • The order of keys has always been dependent on insertion order. – Sven Marnach Mar 20 '12 at 20:09
  • @SvenMarnach Yes, but assuming the same order it was coincidentally identical from one object in one interpreter to another object in the same or different interpreter. This is no the general case. – ephemient Mar 20 '12 at 20:13
2

The policy of keeping keys/items in the same order does not apply between two different dictionary objects and should not be assumed to hold.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

If order is important, you should use an OrderedDict: http://docs.python.org/dev/library/collections.html#collections.OrderedDict . The order of keys in a dictionary can change when key-value pairs are inserted or deleted.

Furthermore, the order can vary between different implementations of the Python language so you cannot rely on the order being the same.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
0

Regular dictionaries in Python are not ordered and you cannot rely on the order of the keys.

In Python 2.7 they introduced OrderedDict. This object stores the keys in the order they were inserted.

If you're using Python 2.6 or less you can have the same functionality by using http://pypi.python.org/pypi/ordereddict

silent1mezzo
  • 2,814
  • 4
  • 26
  • 46