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

Taught in

References