4
exampleDict = {'a':1, 'b':2, 'c':3, 'd':4}

The above dictionary initially iterated through in this order:

b=2
d=4
a=1
c=3

Then, I moved around a ton of files in my code, and now it iterates through in this order:

d=4
a=1
c=3
b=2

I know that the order is internally stored as a hashmap, but what would cause that internal order to change?

Edit: I don't need to preserve order so I will stick with using a dict. I am just wondering why it happened. I thought order wasn't guaranteed, but once it has its arbitrary internal order, it sticks with it for future iterations.

Preethi Vaidyanathan
  • 1,203
  • 1
  • 12
  • 32
  • 1
    this is not the answer to your question but if you care about the order you should not be using a `dict`. – Ma0 Aug 16 '16 at 13:04
  • The order of insertion could definitely cause the order seen during iteration to change. Also, the direction of the wind. Your output is in the same order as before, with just `b=2` moved to the end. – Jonathon Reinhart Aug 16 '16 at 13:04
  • Depending on the order of insertion, the entries can change (due to hash collisions). What I'm curious about is why does it matter? – AndyG Aug 16 '16 at 13:05
  • 1
    dict doesn't guarantee the order, if you want so use https://pymotw.com/2/collections/ordereddict.html – dfranca Aug 16 '16 at 13:06
  • 1
    (in Python 3, the order is literally random, even. You don't even have to change your code, just run it again.) – Wooble Aug 16 '16 at 13:08
  • Maybe, it's unpredictable, so you shouldn't write any code that assumes anything other than that about the order of the items in a dictionary. – martineau Aug 16 '16 at 13:16

3 Answers3

5

Dict don't have a fixed order, from documentation

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.

If you really need to keep it ordered, there is an object called OrderedDict :

from collections import OrderedDict
exampleDict = OrderedDict({'a':1, 'b':2, 'c':3, 'd':4})

see also OrderedDict documentation here

CoMartel
  • 3,521
  • 4
  • 25
  • 48
0

Curious about why you want them ordered. Any chance it's just for consistent logging/printing or similar? If so, you can sort the keys alphabetically. See other article: How to sort dictionary by key in numerical order Python

Community
  • 1
  • 1
TinyTheBrontosaurus
  • 4,010
  • 6
  • 21
  • 34
-1

Yes. If you do change the code between different calls to the dict, the order of iteration will change.

From the docs

If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.

If the order of insertion matter, check out the OrderedDict class

RahulHP
  • 336
  • 4
  • 6
  • *"Yes. If you do change the code between different calls to the dict, the order of iteration will change."* Please tell me where it is defined, and why, calling e.g. `time.time()` would cause an arbitrary dict order to change. – Jonathon Reinhart Aug 16 '16 at 13:06
  • Thanks, my sentence should have been clearer. If you modify the dictionary, the order will change. – RahulHP Aug 16 '16 at 13:07