I need my code to always iterate over the dict items (through dict.keys()
) in the order they were instantiated. However I noticed that when you instantiate a dict in Python, it will NOT be ordered:
>>> dict = {'a':'a', 'b':'b', 'c':'c'}
>>> dict
{'a': 'a', 'c': 'c', 'b': 'b'}
>>> for x in dict.keys():
... print x
a
c
b
How can I guarantee that whenever I iterate through this dict, it will always be in the same order that I created it? And why does Python seem to "scramble" the values whenever I instantiate a new dict?
Note that the order that I talk about here is NOT necessarily alphabetical, numerical or of any other sortable kind.