You should probably use a defaultdict here, it would be more concise