Check if a value exists in the dictionary using any() function

This is the method by which this problem can be solved. In this, we iterate through the whole dictionary using list comprehension and check for each key’s values for a match using a conditional statement. 

Python3




test_dict = {'gfg': 1, 'is': 2, 'best': 3}
 
# Check if key exist in dictionary using any()
if any([True for k,v in test_dict.items() if v == 21]):
    print(f"Yes, It exists in dictionary")
else:
    print(f"No, It doesn't exists in dictionary")


Output

No, It doesn't exists in dictionary

Python | Test if element is dictionary value

Sometimes, while working with a Python dictionary, we have a specific use case in which we just need to find if a particular value is present in the dictionary as it’s any key’s value. This can have use cases in any field of programming one can think of. Let’s discuss certain ways in which this problem can be solved using Python

Similar Reads

Check if a value exists in the dictionary using any() function

This is the method by which this problem can be solved. In this, we iterate through the whole dictionary using list comprehension and check for each key’s values for a match using a conditional statement....

Check if a value exists in the dictionary using a loop

...

Check if a value exists in the dictionary using in operator and values()

This is the brute way in which this problem can be solved. In this, we iterate through the whole dictionary using loops and check for each key’s values for a match using a conditional statement....

Using the get() method of a dictionary

...

Contact Us