Skip to main content

← All terms Β· Standard library

collections.defaultdict

Dict that auto-creates missing keys with a factory: `defaultdict(list)` returns `[]` for any new key. Replaces `if k not in d: d[k] = []`-style code in counters and grouping.

counts = defaultdict(int)
counts[word] += 1

Learn this interactively:

Open lesson iv-43 β†’

Related β€” Standard library

pathlib.PathObject-oriented filesystem paths. `Path(".").rglob("*.py")` beats `os.walk + os.…collections.CounterDict subclass that counts hashable items: `Counter("hello")` β†’ `{'l': 2, ...}`.itertoolsIterator building blocks β€” `chain`, `groupby`, `combinations`, `accumulate`. Laz…functools.cacheMemoize a pure function with `@functools.cache`. Replaces hand-rolled dict cachi…dataclasses.fieldConfigure a dataclass attribute β€” mutable defaults, init/repr behavior. Use `fie…