In Python, a dictionary is Pythons version of a map (associative array or hash table). It lets you map keys to values. This makes it easy to request a value given a key.
A hash table contains both keys and values, but they are in a pseudo random order. This is different from a list (array), that only contains elements.
the good news: a dict in Python is now in insertion order
Dicts are ordered (for newest Python version)
In the newest version of Python, a dictionary is now in insertion order. That has not always been the case.
➜ ~ python3.7
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> d = {'one': True, 'two': True, 'three': True, 'four': True, 'five': True}
>>> d['two']
True
>>> d['four']
True
>>> d
{'one': True, 'two': True, 'three': True, 'four': True, 'five': True}
>>>
If you used an older version of Python, there was no guarantee that the dictionary is in order.
For example, in the older version of Python (2.x)
~ python2
Python 2.7.17 (default, Nov 7 2019, 10:07:09)
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'one': True, 'two': True, 'three': True, 'four': True, 'five': True}
>>> d
{'four': True, 'three': True, 'five': True, 'two': True, 'one': True}
>>>
Quoting the docs referenced above:
Changed in version 3.7: Dictionary order is guaranteed to be insertion
order. This behavior was an implementation detail of CPython from 3.6.
Related links:
Top comments (1)
Dictionaries being ordered by default makes things so much easier! It's a really amazi g feature. Thanks to the great Raymond Hettinger for getting it passed through. The big thing that really helped getting it merged in was that the updated dictionary is actually faster.