Python | Test if key exists in tuple keys dictionary
Sometimes, while working with dictionary data, we need to check if a particular key is present in dictionary. If keys are elementary, the solution of problem in discussed and easier to solve. But sometimes, we can have tuple as key of dictionary. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using any() + generator expression Combination of above functionalities can be used to perform this task. In this, we check for each element inside each key for the target key. The any() is used to check in any keys of dictionary.
Python3
# Python3 code to demonstrate working of # Test if key exists in tuple keys dictionary # using any() + generator expression # initialize dictionary test_dict = {( 4 , 5 ) : '1' , ( 8 , 9 ) : '2' , ( 10 , 11 ) : '3' } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Test key key = 10 # Test if key exists in tuple keys dictionary # using any() + generator expression res = any (key in sub for sub in test_dict) # printing result print ( "Does key exists in dictionary? : " + str (res)) |
The original dictionary : {(4, 5): '1', (8, 9): '2', (10, 11): '3'} Does key exists in dictionary? : True
Method #2 : Using from_iterable() This task can also be performed using this function. In this, we flatten the keys and then check for existence.
Python3
# Python3 code to demonstrate working of # Test if key exists in tuple keys dictionary # using from_iterable() from itertools import chain # initialize dictionary test_dict = {( 4 , 5 ) : '1' , ( 8 , 9 ) : '2' , ( 10 , 11 ) : '3' } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Test key key = 10 # Test if key exists in tuple keys dictionary # using from_iterable() res = key in chain.from_iterable(test_dict) # printing result print ( "Does key exists in dictionary? : " + str (res)) |
The original dictionary : {(4, 5): '1', (8, 9): '2', (10, 11): '3'} Does key exists in dictionary? : True
Method #3 : Using set() + list comprehension
This approach is more efficient than the previous ones as it leverages the fast membership checking of sets. In this method, we convert the keys of the dictionary to a set and then check for the existence of the key using list comprehension.
Python3
# Python3 code to demonstrate working of # Test if key exists in tuple keys dictionary # using set() + list comprehension # initialize dictionary test_dict = {( 4 , 5 ) : '1' , ( 8 , 9 ) : '2' , ( 10 , 11 ) : '3' } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Test key key = 10 # Test if key exists in tuple keys dictionary # using set() + list comprehension keys = set (key for sub in test_dict for key in sub) res = key in keys # printing result print ( "Does key exists in dictionary? : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The original dictionary : {(4, 5): '1', (8, 9): '2', (10, 11): '3'} Does key exists in dictionary? : True
This approach uses the concept of set, which stores only unique elements, and list comprehension to iterate over the tuple keys, and extract all the elements inside the tuple and store it as a set. Then it checks if the target key is present in the set. Time complexity of this approach is O(n) where n is the number of keys in the dictionary, since we are iterating over all the keys in the dictionary to form the set. Auxiliary Space is O(n) as well since we are storing all the elements of tuple keys in a set.
Please Login to comment...