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" ) |
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.