1

I was wondering in what order does the dictionary in python store key : value pairs. I wrote the following in my python shell but I can't figure out what is the reason for the order it is storing the key : value pairs.

>>> d = {}
>>> d['a'] = 8
>>> d['b'] = 8
>>> d
{'a': 8, 'b': 8}
>>> d['c'] = 8
>>> d
{'a': 8, 'c': 8, 'b': 8}
>>> d['z'] = 8
>>> d
{'a': 8, 'c': 8, 'b': 8, 'z': 8}
>>> d['w'] = 8
>>> d
{'a': 8, 'c': 8, 'b': 8, 'z': 8, 'w': 8}

I also tried the same thing with different values for the same keys. But the order remained the same. Adding one more key : value pair gives another result that just can't make out. Here it is :

>>> d[1] = 8
>>> d
{'a': 8, 1: 8, 'c': 8, 'b': 8, 'w': 8, 'z': 8}
Indradhanush Gupta
  • 4,067
  • 10
  • 44
  • 60
  • There is no guaranteed ordering; insertions and deletions and the key values determine the ordering. – Martijn Pieters May 25 '13 at 18:23
  • dictionaries are unordered in python, where as lists and tuples are, @Indradhanush Gupta you got a good link in comment – Grijesh Chauhan May 25 '13 at 18:23
  • @MartijnPieters I used another dictionary named c. It had the same ordering. – Indradhanush Gupta May 25 '13 at 18:24
  • @IndradhanushGupta that is what Martijn says No guaranteed! :) – Grijesh Chauhan May 25 '13 at 18:26
  • @GrijeshChauhan That means one day it could have this order and some other day a different order? – Indradhanush Gupta May 25 '13 at 18:27
  • 3
    @IndradhanushGupta: Sure, replaying the same insertions means you see the same order again. But if you add a few deletions, then you *could* end up with a different order. Use a different Python version or different Python implementation (IronPython, Jython, PyPy) and you could get a different order altogether. – Martijn Pieters May 25 '13 at 18:28
  • 2
    @IndradhanushGupta: See [Why the order in Python dictionaries is arbitrary?](http://stackoverflow.com/a/15479974) – Martijn Pieters May 25 '13 at 18:29
  • 1
    And with recent changes in CPython, you don't even need to change anything. Just running the same program with the same parameters with the same Python implementation can yield different orders due to hash randomization. –  May 25 '13 at 18:33

4 Answers4

3

The short answer is: in an implementation-defined order. You can't rely and shouldn't expect any particular order, and it can change after changing the dictionary in a supposedly-irrelevant manner.

Although not directly, it's somehow explained in Dictionary view objects:

Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

Michał Górny
  • 18,713
  • 5
  • 53
  • 76
1

Elements are stored based on the hash of their key. The documentation states that a key must be a hashable type.

Richard
  • 29,854
  • 11
  • 77
  • 120
1

Dictionaries do not have a predictable order as their keys are stored by a hash. If you need order, use a list or collections.OrderedDict.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1

It's a hash table. The keys are partially ordered by their hash value hash(key), but the actual traversal order of the dictionary can depend on the order that elements were inserted, the number of elements in the dictionary, and possibly other factors. You should never count on it being anything in particular.

hobbs
  • 223,387
  • 19
  • 210
  • 288