+ 2
Can we search for the values in a dictionary using in and not in?
4 Respostas
+ 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.