1

Say I have a dictionary like this:

animals_dict = 
{'cat': ['black', 'white', 'orange'],
'dog': ['black', 'white'],
'parrot': ['orange', 'green']}

And I want to get this:

colors_dict = 
{'black': ['cat', 'dog'],
'white': ['cat', 'dog'],
'orange': ['cat', 'parrot'],
'green': ['parrot']}

The obvious way is to cycle through each animal in animals_dict; cycle through each of its colors; and add the animal to that color in colors_dict. But if you have tens of millions of animals and each animal can have up to a hundred colors, this will get computationally expensive. Is there a more efficient way to achieve this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
TKR
  • 185
  • 1
  • 2
  • 11
  • 1
    Not really. You could possibly convert your dict to a `pandas` `DataFrame` and transpose the resulting dataframe, but the performance gain there comes from doing those calculations outside python rather than from a more efficient approach – Pranav Hosangadi May 16 '22 at 19:57
  • 1
    No; you have a graph with a billion edges, and each of them needs to be reversed. – chepner May 16 '22 at 19:57
  • https://stackoverflow.com/questions/483666/reverse-invert-a-dictionary-mapping Is this what you're looking for? – Bacteria May 16 '22 at 20:01
  • 1
    @Bacteria - Those answers only work for one-to-one mappings or are the trivial solution mentioned in the question. – Michael Szczesny May 16 '22 at 20:05
  • How many unique colors exist? Maybe convert them to numbers will speed up the computation. – Andrej Kesely May 16 '22 at 20:09
  • @AndrejKesely There are millions of unique colors, though each animal will only have a small subset. – TKR May 16 '22 at 20:10
  • The values don't matter; it's the number of pairs induced by number of animals and colors that makes this slow. – chepner May 16 '22 at 20:13
  • 1
    Where did you get the dictionary in the first place? Can you generate the two unidirectional mappings at the same time? – chepner May 16 '22 at 20:14
  • What's the average number of colors for each animal? – Kelly Bundy May 16 '22 at 21:12
  • @KellyBundy Probably in the low two figures, but there's a lot of variation -- some have hundreds. – TKR May 16 '22 at 21:18

1 Answers1

3
index = {'cat': ['black', 'white', 'orange'],'dog': ['black', 'white'],'parrot': ['orange', 'green']}

new_dic = {}
for k,v in index.items():
    for x in v:
        new_dic.setdefault(x,[]).append(k)
print(new_dic)

This gives

{
    'black': ['cat', 'dog'], 
    'white': ['cat', 'dog'], 
    'orange': ['cat', 'parrot'], 
    'green': ['parrot']
}

pythonic way to reverse a dict where values are lists? / How to reverse a dictionary (whose values are lists) in Python?