Python has a tiny magic box called defaultdict. It lives in the collections module. It helps you avoid boring checks like “does this key exist?” before adding values. It is like a normal dictionary, but friendlier.
TLDR: defaultdict creates a default value when a missing key is used. For example, defaultdict(int) starts missing counts at 0, so counts[...