Use a defaultdict rather than "if ... in" pattern.
Replace "if not...else" pattern with "if...else".