What is Python's collections module?
Answer
The collections module provides specialized container types. namedtuple: tuple subclass with named fields — Point = namedtuple("Point", ["x", "y"]); p = Point(1, 2); p.x. deque: double-ended queue with O(1) append and popleft — better than list for queue operations. defaultdict: dict that calls a factory for missing keys — defaultdict(list) auto-creates empty lists. Counter: counts hashable objects — Counter("hello") → {"l": 2, "h": 1, "e": 1, "o": 1}; supports most_common(n). OrderedDict: dict that remembers insertion order (less useful in Python 3.7+ where all dicts are ordered). ChainMap: combines multiple dicts into a single view, looking up keys through the chain. UserDict/UserList/UserString: base classes for creating dict/list/string subclasses.
Previous
What is Python's functools module?
Next
What is the GIL (Global Interpreter Lock) in Python?