Iman Kermani Iman Kermani - 24 days ago 8
Python Question

Dictionary order of items index changes automatically during runtime

The order of index of the items in the dictionary that I assigned some Key/Value data to it in the constructor of my class as one of the class fields, changed automatically during runtime every time I run the program.

I also checked the program using Cython debugger and the interesting thing was actually the python itself change the index of items!

I'm using Python3.5.4

I put a breakpoint exactly after the line of dictionary definition and
here is a picture of this problem.

Answer Source

Dictionaries don't preserve order (i.e. if you iterate over the keys or values of a dict, they don't necessarily show up in the same order as when you defined the dictionary)! If you want to preserve the order you have to use collections.OrderedDict.