August 2018
Intermediate to advanced
366 pages
10h 14m
English
OrderedDict stores both a mapping of the keys to their values and a list of keys that is used to preserve the order of them.
So whenever your look for a key, the lookup goes through the mapping, but whenever you want to list the keys or iterate over the container, you go through the list of keys to ensure they are processed in the order they were inserted in.
The main problem when using OrderedDict is that Python on versions before 3.6 didn't guarantee any specific order of keyword arguments:
>>> attrs = OrderedDict(id="header", style="background-color:red")
This would have again introduced a totally random order of keys even though OrderedDict was used. Not because OrderedDict didn't preserve the order of those keys, but ...