Check if a value exists in the dictionary using a loop

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. 

Python3




# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Test if element is dictionary value
# Using loops
res = False
for key in test_dict:
    if(test_dict[key] == 3):
        res = True
        break
 
# printing result
print("Is 3 present in dictionary : " + str(res))


Output :

The original dictionary is : {'best': 3, 'is': 2, 'gfg': 1}
Is 3 present in dictionary : True

Time complexity: O(N), where N is the number of key-value pairs in the dictionary. 
Auxiliary space: O(1).

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