Usage:

class collections.defaultdict([default_factory[, …]])

Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class and is not documented here.

The first argument provides the initial value for the default_factory attribute; it defaults to None. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.

Example:

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

Normally, trying to access keys that do not exist in dict will cause KeyError. For instance

1
2
3
4
5
6
strings = ('puppy', 'kitten', 'puppy', 'puppy',
'weasel', 'puppy', 'kitten', 'puppy')
counts = {}

for kw in strings:
counts[kw] += 1

It tries to count the number of strings in strings tuple, however, the program will throw error message like

KeyError: ‘puppy’

because there does not exist default values in python dict class.
To overcome this issue, use defaultdict class

1
2
3
4
5
from collections import defaultdict
dd = defaultdict()

for kw in strings:
dd[kw] += 1