Looping Python dictionaries in entered sequence
I am new in Python 开发者_如何学Pythonand 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?
Dictionaries are inherently unordered; it's what gives them (amortized) O(1) lookups for keys.
In more recent versions of python, there is collections.OrderedDict
which does preserve insertion order. Or, you could just keep a separate list.
精彩评论