How does collections.defaultdict work?

collections.defaultdict is a subclass of the built-in dict class in Python. It allows you to specify a default value for a nonexistent key, so that if you try to access a key that does not exist, the default value is returned instead of raising a KeyError.

Watch a course Python - The Practical Guide

Here is an example of how to use defaultdict to count the occurrences of words in a sentence:

from collections import defaultdict

sentence = "The quick brown fox jumps over the lazy dog"
words = sentence.split()

word_counts = defaultdict(int)  # default value of nonexistent keys is 0

for word in words:
    word_counts[word] += 1

print(word_counts)

This will output:

defaultdict(, {'The': 1, 'quick': 1, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'the': 1, 'lazy': 1, 'dog': 1})

You can also use a function as the default_factory, like this:

from collections import defaultdict

s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list)
for k, v in s:
    d[k].append(v)
print(d)

This will output:

defaultdict(, {'yellow': [1, 3], 'blue': [2, 4], 'red': [1]})

In this example, the default_factory is list, so if a key is not found, an empty list is returned.