Collections
Python's `collections` module provides specialized container datatypes that extend the built-in dict, list, set, and tuple. They solve common programming patterns with more efficiency and clearer code. The guide walks through Counter, defaultdict, deque, ChainMap, OrderedDict. `Counter(iterable)` counts hashable items: `Counter("abracadabra")` returns `{'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}`. Methods: `.most_common(n)` returns top n items, `.elements()` returns all elements repeating counts, `.subtract(other)` decrements counts, `+`/`-` combine counters. `defaultdict(factory)` is a dict that calls `factory()` for missing keys. Examples: `defaultdict(int)` defaults to 0 for counting, `defaultdict(list)` defaults to empty list for grouping, `defaultdict(set)` defaults to empty set. Access a missing key creates the default value automatically. `deque(iterable, maxlen=N)` is a double-ended queue optimized for fast appends and pops from both ends. Methods: `.append(x)` right, `.appendleft(x)` left, `.pop()` right, `.popleft()` left, `.extend(iterable)` right, `.extendleft(iterable)` left, `.rotate(n)` rotates elements. The guide is organized into 5 sections that build on each other, each pairing a prose explanation with runnable code.