Learn: Collections¶
Four notebooks, in order. Each is self-contained, but together they cover the whole module — one container per notebook (with the smaller OrderedDict and ChainMap folded into the last).
Notebooks in this section¶
- Counter — counting hashable things. Building a
Counterfrom any iterable,most_common, the arithmetic operators (+,-,&,|),update/subtract,total, andelements. - defaultdict — a
dictthat supplies a default for missing keys. The grouping pattern (list), the counting pattern (int), sets and nesting, and how it compares todict.setdefault. - deque — a double-ended queue with fast appends and pops at both ends. Why
list.pop(0)is slow,appendleft/popleft, bounded deques withmaxlenfor sliding windows and history, androtate. - namedtuple and friends — tuples with named fields: defining them,
_replace,_asdict,_fields, and how they sit between a tuple and a class. ThenOrderedDict(what it still offers now thatdictkeeps order) andChainMap(layered lookups for configs and defaults).
After these, the Recipes cover task-focused applications and the Reference has quick lookups for every type.