7

Does the python code

for key in dict:
    ...

, where dict is a dict data type, always iterate in a fixed order with regrard to key? For example, suppose dict={"aaa":1,"bbb",2}, will the above code always first let key="aaa" and then key="bbb" (or in another fixed order)? Is it possible that the order is random? I am using python 3.3 in ubuntu 13 and let's assume this running environment doesn't change. Thank you.

add one thing: during multiple runs, the variable dict remains unchanged, i.e., generate once and read multiple times.

user2384994
  • 1,759
  • 4
  • 24
  • 29
  • 1
    Orders of dict keys is random, http://stackoverflow.com/questions/5629023/key-order-in-python-dicionaries – alexvassel Oct 10 '13 at 06:21
  • 2
    you can try [OrderedDict](http://docs.python.org/dev/library/collections.html#collections.OrderedDict) –  Oct 10 '13 at 06:23
  • 1
    @alexvassel: The order is unordered, but not random (for it is given by the implementation of the dict). – Matthias Oct 10 '13 at 06:45

2 Answers2

12

Intrinsically, a dictionary has no order in which it stores it keys. So you can not rely on the order. (I wouldn't assume the order to be unchanged even when the environment is identical).

One of the few reliable ways:

for key in sorted(yourDictionary.keys()):
    # Use as key and yourDictionary[key]

EDIT: Response to your comment: Python does not store keys in a random fashion. All the documentation says is that, you should not rely on this order. It depends on the implementation how the keys are ordered. What I will say here about your question is: If you are relying on this order, you are probably doing something wrong. In general you should/need not rely on this at all. :-)

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
  • Thank you. I forgot one thing that may be important and I add it in the original post. In my understanding, to be random needs more work, if dict remains unchanged, does its internal structure still keep varying all the time? – user2384994 Oct 10 '13 at 06:28
  • Yes, to be random does need more work. It isn't random, but it's not in any regular order that you can rely on. – Pandu Oct 10 '13 at 06:34
  • My question is restricted to only one fixed implementation, fixed running environment. If that's fixed, that's enough for me. Now may I have a positive answer to my question? – user2384994 Oct 10 '13 at 06:39
  • I have a particular program with a dict that gives the same order every time, *in one run*. The next time I run, with the same data, the order is different. – Rick Berge Nov 03 '15 at 18:03
  • update:this answer is right,but something is outdate,look this: https://stackoverflow.com/a/40007169/8026780 – Cherrymelon Nov 09 '21 at 03:02
5
CPython implementation detail: Keys and values are listed in an arbitrary 
order which is non-random, varies across Python implementations, and 
depends on the dictionary’s history of insertions and deletions.

For more info:http://docs.python.org/2/library/stdtypes.html#dict.items

What's more, You could use collections.OrderedDict to make the order fixed.

atupal
  • 16,404
  • 5
  • 31
  • 42
  • so, if I restricted myself to one python implementation and the variable remains unchanged since it's created, the order should be fixed, right? Because I think to change the key's order needs computer's extra work. – user2384994 Oct 10 '13 at 06:32
  • yes, the order will be fixed if the dictionary has no insertions and deletions. – atupal Oct 10 '13 at 06:38