Open In App

Python – Test if element is part of dictionary

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a dictionary, test if K is part of keys or values of dictionary.

Input : test_dict = {“Gfg” : 1, “is” : 3, “Best” : 2}, K = “Best” 
Output : True 
Explanation : “Best” is present in Dictionary as Key. 

Input : test_dict = {“Gfg” : 1, “is” : 3, “Best” : 2}, K = “Geeks” 
Output : False 
Explanation : “Geeks” is not present in Dictionary as Key.

Method #1 : Using any() + items() + generator expression 

The combination of above functions can be used to solve this problem. In this, we check for any element using any() and items() is used to extract all the keys and values of dictionary.

Python3




# Python3 code to demonstrate working of
# Test if element is part of dictionary
# Using any() + generator expression + items()
 
# initializing dictionary
test_dict = {"Gfg" : 1, "is" 3, "Best" : 2}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = "Gfg"
 
# using any() to check for both keys and values
res = any(K == key or K == val for key, val in test_dict.items())
 
# printing result
print("Is K present in dictionary? : " + str(res))


Output

The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
Is K present in dictionary? : True

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

Method #2 : Using chain.from_iterables() + items()

The combination of above functions can be used to solve this problem. In this, we flatten all the items and then check if K is present is any of the items().

Python3




# Python3 code to demonstrate working of
# Test if element is part of dictionary
# Using chain.from_iterables() + items()
from itertools import chain
 
# initializing dictionary
test_dict = {"Gfg" : 1, "is" 3, "Best" : 2}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = "Gfg"
 
# flattening key-values and then checking
# using in operator
res = K in chain.from_iterable(test_dict.items())
 
# printing result
print("Is K present in dictionary? : " + str(res))


Output

The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
Is K present in dictionary? : True

Method #3: Using the “in” operator to check if the key is present in the dictionary.

In this approach, we don’t need to iterate through the dictionary using a generator expression or the items() method. we can simply use the in operator to check if the key is present in the dictionary.

Python3




# Python3 code to demonstrate working of
# Test if element is part of dictionary
# Using in operator
 
# initializing dictionary
test_dict = {"Gfg" : 1, "is" 3, "Best" : 2}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = "Gfg"
 
# using "in" operator to check if key is present in the dictionary
res = K in test_dict
 
# printing result
print("Is K present in dictionary? : " + str(res))


Output

The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
Is K present in dictionary? : True

Time complexity: O(1), which is constant time. This is because dictionary in Python is implemented as a hash table, and the “in” operator uses hash table lookups to find the key, which takes constant time on average.
Auxiliary space: O(1), as it doesn’t require any additional space to store intermediate results or data structures. The only space used is for the dictionary itself, which is already given as input.

Method #4: Using the get() method to check if the key is present in the dictionary.

  1. The program initializes a dictionary named test_dict with the keys “Gfg”, “is”, and “Best”, and their corresponding values.
  2. The program then prints the original dictionary using the print() function.
  3. The program initializes a variable named K with the value “Gfg”.
  4. The program uses the get() method to check if the key K is present in the test_dict.
  5. The get() method returns the value of the key K if it is present in the dictionary. Otherwise, it returns None.
  6. The program checks if the value returned by get() method is not None.
  7. If the value returned by get() method is not None, then it prints “Is K present in dictionary? : True”.
  8. Otherwise, it prints “Is K present in dictionary? : False”.

Python3




# Python3 code to demonstrate working of
# Test if element is part of dictionary
# Using get() method
 
# initializing dictionary
test_dict = {"Gfg" : 1, "is" 3, "Best" : 2}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = "Gfg"
 
# using get() method to check if key is present in the dictionary
res = test_dict.get(K)
 
# checking if res is not None, which means the key is present
if res is not None:
    print("Is K present in dictionary? : True")
else:
    print("Is K present in dictionary? : False")


Output

The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
Is K present in dictionary? : True

Time complexity: O(1)
Auxiliary space: O(1)

Method 5: Using a for loop to check if the key is present in the dictionary

Step-by-step approach:

  • Initialize the dictionary
  • Print the original dictionary
  • Initialize the key ‘K’
  • Set a flag variable to False
  • Iterate through the keys in the dictionary using a for loop
  • If the current key is equal to the search key, set the flag variable to True and break out of the loop
  • Check the value of the flag variable
  • If the flag variable is True, print “Is K present in dictionary? : True”
  • If the flag variable is False, print “Is K present in dictionary? : False”

Python3




# Python3 code to demonstrate working of
# Test if element is part of dictionary
# Using for loop
 
# initializing dictionary
test_dict = {"Gfg" : 1, "is" 3, "Best" : 2}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = "Gfg"
 
# using for loop to check if key is present in the dictionary
flag = False
for key in test_dict:
    if key == K:
        flag = True
        break
 
if flag:
    print("Is K present in dictionary? : True")
else:
    print("Is K present in dictionary? : False")


Output

The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
Is K present in dictionary? : True

Time complexity: O(n)
Auxiliary space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads