0

If user enters something which is in a dictionary and that thing is already in a dictionary, then how to print in python..

d2={"Tomato":"Tamatar", "Carrot":"Gajar", "Potato":"Aloo", "Junk Food":{"Bhajiya Paw":"bp","Paw Bhaji":"PB","Vada Paw":"vp"}} meaning=input("enter the name :") And now if user enters Bhajiya Paw .?

24th Aug 2020, 9:40 AM
Akash Dubey
Akash Dubey - avatar
6 Respuestas
+ 7
Sorry to say ~ swim ~ , but the dict we have as sample is a nested dict, so the code does not work: if 'Bhajiya Paw' in d2.keys(): # can not find this key, as it is a nested dict print('Bhajiya Paw') print(d2["Bhajiya Paw"]) #will print "bp" => keyError ! The code to check for 'Bhajiya Paw' should be like this: ... for inner in d2.items(): if meaning in inner[1]: print('meaning already exists') ...
24th Aug 2020, 6:04 PM
Lothar
Lothar - avatar
+ 6
I have prepared a file with some samples, to give you a better understanding how it works. Simple dicts with key / values as int, float or string is not a problem. But in our case we have an other (nested) dictionary as 'value', this is a bit special. And sorry for the delay. https://code.sololearn.com/c7B23xPgT559/?ref=app
25th Aug 2020, 4:58 PM
Lothar
Lothar - avatar
+ 5
Akash Dubey , what do you want to input, and what do you like to get back? Just give me a short sample. Thanks!
24th Aug 2020, 6:49 PM
Lothar
Lothar - avatar
0
Lothar is not there any way other than you have mention....to call the word from the nested dictionary.?
24th Aug 2020, 6:12 PM
Akash Dubey
Akash Dubey - avatar