Gotta be careful of that groupby. Using defaultdict has the pitfall that it continues to insert new keys when looking up missing keys after you're done grouping. Sometimes it's better to use a regular dict and setdefault during the grouping, ensuring that, after the grouping, missing lookups will properly raise KeyError.
In fact, AFAIK, Norvig made that specific mistake previously.
In fact, AFAIK, Norvig made that specific mistake previously.