defaultdict
A dictionary from the collections module that creates a default value, such as an empty list, the first time a missing key is used.
In more detail
groups = defaultdict(list) lets you write groups[key].append(item) without first checking whether the key exists. The argument is a function that makes the default, such as list, int or set.
Related terms
- collections module — The standard library module of extra container types, including Counter, defaultdict, namedtuple and deque.
- dictionary — A Python collection that stores values under keys, written in curly brackets, such as {"tea": 250}.
- KeyError — The Python exception raised when you look up a dictionary key that does not exist.
Taught in
- A Standard Library Tour — Hands-on Python: Complete Video Course & Book