Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to invert dictionary with multiple values of each key?

I have been working on this forever. I have the following dictionary.

Subject1='Math'
Subject2='English'
Subject3='Chemstry'
Subject4='Physics'
Subject5='Geology'
Subject6='PE'
Subject7='Music'
Subject8='Psychology'
Subject9='Politics'
Subject10='Acting'

dict_1 = {(Subject1, Subject2):['Michael','James','Lydia'],
          (Subject3, Subject4):['Michael','Lydia','James'],
          (Subject5, Subject6):['Tom'],
          (Subject7, Subject8):[],
          (Subject9, Subject10):[]
}

I want to inverse the dictionary so that it will look like:

{['Michael', 'James', 'Lydia']: {(Subject1, Subject2), (Subject3, Subject4)}, 
 ['Tom']: {(Subject5, Subject6)}, 
 []: {(Subject7, Subject8), (Subject9, Subject10)} }

I tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from collections import defaultdict

new_tel = defaultdict(list)
for key, value in dict_1.items():
    new_tel[value].append(key)

This worked for hashable type but not unhashable type list. Maybe I can join the string of ['Michael','James','Lydia'] to 'James Lydia Michael‘ so that it can be a valid key? How do I sort the elements in the list ['Michael','James','Lydia'] and ['Michael','Lydia','James'] so that they are in alphabetical order before I join them?

This is a more achievable output:

{'James Lydia Michael': {(Subject1, Subject2), (Subject3, Subject4)}, 
 'Tom': {(Subject5, Subject6)}, 
 'None': {(Subject7, Subject8), (Subject9, Subject10)} }

>Solution :

Dictionary keys can’t be lists but it can be tuples. So you can do something like below. Sort the values, make them tuples to use as keys and use dict.setdefault:

out = {}
for k,v in dict_1.items():
    out.setdefault(tuple(sorted(v)), set()).add(k)

Output:

{('James', 'Lydia', 'Michael'): {('Chemstry', 'Physics'), ('Math', 'English')},
 ('Tom',): {('Geology', 'PE')},
 (): {('Music', 'Psychology'), ('Politics', 'Acting')}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading