Python | Test if element is dictionary value
Last Updated :
17 Apr, 2023
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.
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 }
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
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
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ( "The original dictionary is : " + str (test_dict))
res = False
for key in test_dict:
if (test_dict[key] = = 3 ):
res = True
break
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).
Check if a value exists in the dictionary using in operator and values()
This task can be performed by utilizing the above functionalities. The in operator can be used to get the true value of presence and the values function is required to extract all values of dictionaries.
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ( "The original dictionary is : " + str (test_dict))
res = 3 in test_dict.values()
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 values in the dictionary test_dict.
Auxiliary Space: O(1), as no extra space is used.
Using the get() method of a dictionary
The get() method returns the value for a given key if it exists in the dictionary. If the key does not exist, it returns None. You can use this behavior to check if a value exists in the dictionary by checking if it is in the list returned by values().
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
if 21 in test_dict.values():
print (f "Yes, It exists in dictionary" )
else :
print (f "No, It doesn't exist in dictionary" )
|
Output
No, It doesn't exist in dictionary
Time complexity: O(n)
Auxiliary space: O(1)
Using isinstance() function:
Approach:
Check if the input value is of dictionary type or not.
If it is, check if the given element exists in any of the dictionary values.
If yes, return True. Else, return False.
Python3
def check_dict_value_1(d, val):
if isinstance (d, dict ):
return any (val = = v for v in d.values())
return False
my_dict = { 'a' : 1 , 'b' : 2 , 'c' : { 'd' : 3 , 'e' : 4 }, 'f' : 5 }
my_value = 4
result = check_dict_value_1(my_dict, my_value)
print (result)
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Please Login to comment...