18
>>> D = {'a': 1, 'b': 2, 'c': 3}
>>> D
{'a': 1, 'c': 3, 'b': 2}

I just did this in the Python shell and I'm just wondering why the key 'c' would be after the key 'b'???

Matthew Schinckel
  • 35,041
  • 6
  • 86
  • 121
Jackyjjc
  • 552
  • 1
  • 3
  • 13

4 Answers4

17

The order has to do with how they work internally and what order they end up in the hashtable. That in turn depends on the keys hash-value, the order they were inserted, and which Python implementation you are using.

The order is arbitrary (but not random) and it will never be useful to know which order it will be.

To get a sorted list of keys, just use sorted(D), which in your case will return ['a', 'b', 'c'].

serenesat
  • 4,611
  • 10
  • 37
  • 53
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
7

In Python 2.7 you can use Ordered Dict.

Fábio Diniz
  • 10,077
  • 3
  • 38
  • 45
5

From the documentation:

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • Thank you and I knew dictionary is unordered. So the things python did just confuse me because it changed the display order. – Jackyjjc Dec 16 '10 at 06:55
  • 2
    It's made explicit that the order can change at any time. That's so that the implementation can be faster. If you need the keys in a particular order, you can sort them. – Optimal Cynic Dec 16 '10 at 07:01
  • 2
    QQending: unordered means (among other things) the ordering you get out might not be the order you put in, so why is pythons's behavior confusing? If you're wondering why it did something other than "sorted" order, dicts use hash tables, not search trees. The ordering is probably going to be based on the hash of the key mod the current size of the table or something equally unpredictable. – Laurence Gonsalves Dec 16 '10 at 07:09
  • Laurence Gonsalves: Thank you for your detailed explaination. I just began to learn python and I tend to make everything clear in my mind so I posted this question. – Jackyjjc Dec 16 '10 at 07:20
  • I was constantly arguing with (or at least correcting quietly) one of my lecturers last semester about unsorted versus unordered; she kept on saying unordered when she meant unsorted. `[1,7,3,2,5]` is unsorted but it *is* ordered. `set` and `dict` are unordered, `list` and `tuple` are ordered (and may or may not be sorted). – Chris Morgan Dec 16 '10 at 07:46
2

In any order it pleases. Such is the nature of a dictionary. If you want it in a specific order, you have to do that yourself:

>>> d = {'pax': 1, 'george': 2, 'guido' : 3}

>>> d
{'pax': 1, 'george': 2, 'guido': 3}

>>> [(key,d[key]) for key in sorted(d)]
[('george', 2), ('guido', 3), ('pax', 1)]
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Not strictly true: Lennart's answer is technically more correct "The order is arbitrary (but not random)". – Matthew Schinckel Dec 16 '10 at 08:49
  • 1
    Nowhere did I mention "random", just that the dictionary doesn't specify order. It _can_ return them in any order it sees fit. The fact that it may return in a deterministic order based on content and history of operations in no way resolves you from the responsibility of sorting it yourself if that's what you want: "The keys() method of a dictionary object returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the sorted() function to it)". – paxdiablo Dec 16 '10 at 10:16