2

I am trying to create a new dict using a list of values of an existing dict as individual keys.

So for example:

dict1 = dict({'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]})

and I would like to obtain:

dict2 = dict({1:['a','b','c'], 2:['a','b','c'], 3:['a','b'], 4:['b']})

So far, I've not been able to do this in a very clean way. Any suggestions?

Dan
  • 33,953
  • 24
  • 61
  • 87
  • 3
    out of curiosity: why do you need that? – Sven Hecht Sep 11 '09 at 10:31
  • This needs to be accessible in both directions as it'll happen a lot, and it needs to be serialisable, ideally without introducing more classes that would need importing if it was used in a separate module due to a complexity of the way the particular system i'm working with does things. – Dan Sep 11 '09 at 10:59

3 Answers3

8

If you are using Python 2.5 or above, use the defaultdict class from the collections module; a defaultdict automatically creates values on the first access to a missing key, so you can use that here to create the lists for dict2, like this:

from collections import defaultdict
dict1 = dict({'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]})
dict2 = defaultdict(list)
for key, values in dict1.items():
    for value in values:
        # The list for dict2[value] is created automatically
        dict2[value].append(key)

Note that the lists in dict2 will not be in any particular order, as a dictionaries do not order their key-value pairs.

If you want an ordinary dict out at the end that will raise a KeyError for missing keys, just use dict2 = dict(dict2) after the above.

adurdin
  • 1,376
  • 1
  • 10
  • 15
4

Notice that you don't need the dict in your examples: the {} syntax gives you a dict:

dict1 = {'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
-3

Other way:

dict2={}
[[ (dict2.setdefault(i,[]) or 1) and (dict2[i].append(x)) for i in y ] for (x,y) in dict1.items()]  
Domas Mituzas
  • 239
  • 2
  • 4