while oldDict:
    key, valuelist = oldDict.popitem()
for value in valuelist:
    invertedDict[value].append(key)
i += 1
if i % 1000 == 0:
    oldDict[None] = None
del oldDict[None]
return invertedDict
In-place dictionary inversion in Python