Might be as simple as adding
from collections import defaultdict
and then initializing dictOut
as a defaultdict
containing dict
objects:
dictOut = defaultdict(dict)
A defaultdict
is just like a dictionary, but if the key is missing it starts from a default value (in this case, a dictionary) - so the nested dictionary access will work without a KeyError.