October 2018
Beginner to intermediate
398 pages
11h 1m
English
The important thing about ordered dictionaries is that they remember the insertion order, so when we iterate over them, they return values in the order they were inserted. This is in contrast to a normal dictionary, where the order is arbitrary. When we test to see whether two dictionaries are equal, this equality is only based on their keys and values; however, with OrderedDict, the insertion order is also considered an equality test between two OrderedDict objects with the same keys and values, but a different insertion order will return False:
>>> import collections>>> od1= collections.OrderedDict()>>> od1['one'] = 1>>> od1['two'] = 2>>> od2 = collections.OrderedDict()>>> od2['two'] = 2>>> od2['one'] = 1>>> od1==od2 ...