Some snippets of code I demonstrated in class, in no particular order¶

defaultdict¶

Defaultdict allow you to specify a default value in cases that would have raised a KeyError on a normal dict.

In [3]:
from collections import defaultdict
bonus_points = defaultdict(lambda : 0)
# The following works as expected instead of raising a KeyError
bonus_points['B00804592'] = bonus_points['B00804592'] + 1
In [ ]: