Can we search for the values in a dictionary using in and not in? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

Can we search for the values in a dictionary using in and not in?

python

3rd Mar 2018, 11:12 PM
Somnath Mahato
Somnath Mahato - avatar
4 ответов
+ 4
d = {1: “one”, 2: “two”} 1 in d # returns True, works for keys “two” in d.values() # returns True, works for values 2 in d.keys() # returns True, also for keys, might be clearer to read
4th Mar 2018, 9:46 PM
Pedro Demingos
Pedro Demingos - avatar
+ 3
Yes, the values() method returns a view of a dictionary’s values. You can use it like: for v in d.values(): print(v) Though d.values() isn’t a list.
4th Mar 2018, 11:45 PM
Pedro Demingos
Pedro Demingos - avatar
+ 1
So, we just need to add the method values(), in order to search for any value in the dictionary?
4th Mar 2018, 9:50 PM
Somnath Mahato
Somnath Mahato - avatar
+ 1
Perfect! Thank you so much for the explanation, Pedro. You were a nice help.
4th Mar 2018, 11:47 PM
Somnath Mahato
Somnath Mahato - avatar