I am new in Python and while looping through the items of a dictionary structure I discovered that they come up in a different order than the sequence they were entered. That happens in Perl hashes too I think. Is there a simple way to get the items in the “right” order?
Share
Dictionaries are inherently unordered; it’s what gives them (amortized) O(1) lookups for keys.
In more recent versions of python, there is
collections.OrderedDictwhich does preserve insertion order. Or, you could just keep a separate list.