Inverted dictionary | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Inverted dictionary

d = dict() #Crerating the dictionary #Adding items to the dictionary d['job'] = ["Scientist", "Researcher"] d['Field'] = ["Biology", "Chemistry", "Computer science", "Healthsciences"] d['Education'] = ["BSc","MSc","PhD"] d['languages'] = ["French", "English", "Spanish", "Berber", "Arabic", "Turkish","Mandarin", "Japanese"] I have this dictionary and i want to write a function that can invert the dictionary where the function will need to turn each of the list items into separate keys in the inverted dictionary.

6th Aug 2020, 6:26 PM
Louai Belfounes
Louai Belfounes - avatar
22 Answers
+ 20
invDic = lambda x: {i:j for j in x for i in x[j] } print(invDic(d))
6th Aug 2020, 7:13 PM
Louis
Louis - avatar
+ 8
Do you want it like this? #it works for lists too https://code.sololearn.com/cGPKs4c6GcpO/?ref=app
6th Aug 2020, 6:54 PM
Sousou
Sousou - avatar
+ 7
di={} for k in d: di = {**di,**dict.fromkeys(d[k], k)} https://code.sololearn.com/cU814mpd8csY/?ref=app
6th Aug 2020, 10:08 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 6
Oma Falk i said already just need 1 more comprehention or for statement. just 2 rowa instead of 1 written previously https://code.sololearn.com/czugfo1P5oj3/?ref=app works perfectly Louai Belfounes ,
6th Aug 2020, 8:43 PM
george
george - avatar
+ 5
george ... Apply it to the dict in question please. It wont work. also not dict(zip(d.items(),d.values()) Louis answer is perfect .
6th Aug 2020, 8:22 PM
Oma Falk
Oma Falk - avatar
+ 3
george every key can become a value but not every value(in this case a list) can become a key. So I doubt there is sth for it. If you remember might, it would be worth to be poste here...very interesting
6th Aug 2020, 8:18 PM
Oma Falk
Oma Falk - avatar
+ 3
george 👍that's good
6th Aug 2020, 8:49 PM
Oma Falk
Oma Falk - avatar
+ 3
george yeah and finally Louis and you provide the same solution. But he belongs to the Anonymous Pythonistas.
6th Aug 2020, 8:57 PM
Oma Falk
Oma Falk - avatar
+ 3
Why don't you use a two dimensional list or equivalent data structure like [(x,m),(f,g)].....I guess that should work 🤔🤔🤔
8th Aug 2020, 3:28 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
george wont work here
6th Aug 2020, 8:06 PM
Oma Falk
Oma Falk - avatar
+ 2
use dictionary comprehention and items function of sictionary. # just any dictionary dict = { "geo": 1, "maria":2} #main func dict comprehention dict ={value:key for (key,value) in dict.items()} #print result print(dict)
6th Aug 2020, 8:20 PM
george
george - avatar
+ 2
Oma Falk Louai Belfounes also according to question to create function i make changes. Now function takes dictionary and returns inverted dictionary P.S. Relax and solution comes itself )))))
6th Aug 2020, 8:52 PM
george
george - avatar
+ 2
my knowledge in programming is c++ and JS. python just for hobby. lousis provide more beatifull solution than me))
6th Aug 2020, 9:00 PM
george
george - avatar
+ 1
As i remember there is integrated function for inver array dictionary. Please check api of python3
6th Aug 2020, 6:29 PM
george
george - avatar
+ 1
Oma Falk sorry?
6th Aug 2020, 8:08 PM
george
george - avatar
+ 1
in case of list just need 1 more comprehension and it will be ok
6th Aug 2020, 8:21 PM
george
george - avatar
+ 1
Louis Thank you for your answer, but since i'am a beginner, i don't really understand it
8th Aug 2020, 1:05 AM
Louai Belfounes
Louai Belfounes - avatar
+ 1
Sousou Thank you for your contribution
8th Aug 2020, 1:06 AM
Louai Belfounes
Louai Belfounes - avatar
+ 1
george can you please explain exactly what happens when you used the update method ?
8th Aug 2020, 1:07 AM
Louai Belfounes
Louai Belfounes - avatar
+ 1
Louai Belfounes merging of 2 dictionaries
8th Aug 2020, 3:47 AM
george
george - avatar