Posted on September 15, 2020

The defaultdict is a specialized container type which supplies missing values based on the given default type.

from collections import defaultdict

# using defaultdict will return a default value for the missing key
dd = defaultdict(int)
print('defaultdic', dd['item'])

# using a regular dictionary will return a key error
d = {}
print('dict', d['item'])

Further reading


Gavin Wiggins © 2020