How to reverse a dictionary (whose values are lists) in Python? [duplicate]

You can do it very simply like this:

newdict = {}
for key, value in olddict.items():
    for string in value:
        newdict.setdefault(string, []).append(key)

Leave a Comment