Order items in a Python dictionary

I just doubt it ... I created the following dictionary:

>>> alpha={'a': 10, 'b': 5, 'c': 11}

But when I want to see the keys and values โ€‹โ€‹of the dictionary that I received:

>>> alpha
{'a': 10, 'c': 11, 'b': 5}

See that "b" and "c" are reversed. How can I make the position the same as when the dictionary was created?

+5
source share
2 answers

Dictionaries are unordered containers - if you want to keep order, you can use collections.OrderedDict(Python 2.7 or later) or use a different type of container, which, of course, is kept in order.

, , , , ( ), .

+21

All Articles