Part 3 · Section 09
Specialized Dictionaries
A plain dict covers most needs, but a few patterns come up so often that the standard library ships ready-made dictionaries for them: a dictionary that fills in missing keys for you, one that counts things, one that layers several mappings on top of each other, and a base class for writing your own dictionary types safely. All of them live in the collections module.
In this section you’ll learn:
defaultdict: default values produced by a factory function, and how that differs fromdict.getOrderedDict: its extra methods (move_to_end,popitem(last=False)) and order-sensitive equalityOrderedDictvs plaindictnow that every dict keeps insertion order: what you can do without it, and what it still does betterCounter: a multiset withmost_common,elements,update/subtract, arithmetic, comparison operators andtotal()ChainMap: a live, layered view over several mappings, and why writes only touch the first oneUserDict: why subclassingdictdirectly is fragile, and howUserDictfixes it