+ 2
Can we search for the values in a dictionary using in and not in?
4 Answers
+ 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
+ 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.
+ 1
So, we just need to add the method values(), in order to search for any value in the dictionary?
+ 1
Perfect! Thank you so much for the explanation, Pedro. You were a nice help.