While this function works for this use case, [collections.defaultdict](https://docs.python.org/2/library/collections.html#defaultdict-examples) is a more efficient and generic solution.