views:

118

answers:

3

Hi guys! So, I have a dictionary with almost 100,000 (key, values) pairs and the majority of the keys map to the same values. For example imagine something like that:

mydict =  {'a': 1, 'c': 2, 'b': 1, 'e': 2, 'd': 3, 'h': 1, 'j': 3}

What I want to do, is to reverse the dictionary so that each value in mydict is going to be a key at the reverse_dict and is going to map to a list of all the mydict.keys that used to map to that value at the mydict. So based on the example above I would get:

reversed_dict = {1: ['a', 'b', 'h'], 2:['e', 'c'] , 3:['d', 'j']} 

I came up with a solution that is very expensive and I would really want to hear any ideas more efficient than mine.

my expensive solution:

reversed_dict = {}
for value in mydict.values():
    reversed_dict[value] = []
    for key in mydict.keys():
        if mydict[key] == value:
            if key not in reversed_dict[value]: reversed_dict[value].append(key)

Output >> reversed_dict = {1: ['a', 'b', 'h'], 2: ['c', 'e'], 3: ['d', 'j']}

I would really appreciate to hear any ideas better and more efficient than than mine. Thanks!

+3  A: 
from collections import defaultdict
reversed_dict = defaultdict(list)
for key,value in mydict.iteritems():
    reversed_dict[value].append(key)

Please do not use dict as a variable, this collides with function dict()

joaquin
`dict` is a function name, and not a keyword in Python.
Xavier Ho
ooops, corrected thanks
joaquin
Yes, I know, my bad. I corrected it :)Thanks a lot for the answer!
Galois
s/midic/mydic/ to make it work
John Machin
A: 
reversed_dict = collections.defaultdict(list)
for key, value in dict_.iteritems():
  reversed_dict[value].append(key)
Ignacio Vazquez-Abrams
A: 
for k,v in dict.iteritems():
    try:
      reversed_dict[v].append(k)
    except KeyError:
       reversed_dict[v]=[k]
Florian Diesch