I have a dictionary with almost 100,000 (key, value) pairs and the majority of the keys map to the same values. For example:
mydict = {\'a\': 1, \'c\': 2, \'
Using collections.defaultdict:
from collections import defaultdict reversed_dict = defaultdict(list) for key, value in mydict.items(): reversed_dict[value].append(key)