Use defaultdict to avoid writing these if statements.