Python | Check if one dictionary is subset of other
Sometimes, while working with Python, we might have a problem in which we need to find, if a particular dictionary is a part of other i.e it is subset of other. A problem that have a huge potential in web development domain, having knowledge of solving can be useful. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using all() + items() This task can be performed using the combination of above two functions, in which we check for all the items of the subdict with the original dict using all() and fetch it’s each pair using items().
Python3
# Python3 code to demonstrate working of # Check if one dictionary is subset of other # Using all() + items() # Initialize dictionaries test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 } test_dict2 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 } # printing original dictionaries print ("The original dictionary 1 : " + str (test_dict1)) print ("The original dictionary 2 : " + str (test_dict2)) # Using all() + items() # Check if one dictionary is subset of other res = all (test_dict1.get(key, None ) = = val for key, val in test_dict2.items()) # printing result print ("Does dict2 lie in dict1 ? : " + str (res)) |
The original dictionary 1 : {‘CS’: 5, ‘is’: 2, ‘best’: 3, ‘gfg’: 1, ‘for’: 4} The original dictionary 2 : {‘is’: 2, ‘best’: 3, ‘gfg’: 1} Does dict2 lie in dict1 ? : True
Method #2 : Using items() + <= operator Another alternative to perform the above task can be using the items() along with <= operator. This just checks for less than or all values of all the key value matched.
Python3
# Python3 code to demonstrate working of # Check if one dictionary is subset of other # Using items() + <= operator # Initialize dictionaries test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 } test_dict2 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 } # printing original dictionaries print ("The original dictionary 1 : " + str (test_dict1)) print ("The original dictionary 2 : " + str (test_dict2)) # Using items() + <= operator # Check if one dictionary is subset of other res = test_dict2.items() < = test_dict1.items() # printing result print ("Does dict2 lie in dict1 ? : " + str (res)) |
The original dictionary 1 : {‘CS’: 5, ‘is’: 2, ‘best’: 3, ‘gfg’: 1, ‘for’: 4} The original dictionary 2 : {‘is’: 2, ‘best’: 3, ‘gfg’: 1} Does dict2 lie in dict1 ? : True
Method #3 : Using set() + issubset()
This is yet another approach to solve the task, in which we perform the task using set() and issubset(). The set() converts the dictionary into a set, and then issubset() checks for all the values of the subset.
Python3
# Python3 code to demonstrate working of # Check if one dictionary is subset of other # Using set() + issubset() # Initialize dictionaries test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 } test_dict2 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 } # printing original dictionaries print ( "The original dictionary 1 : " + str (test_dict1)) print ( "The original dictionary 2 : " + str (test_dict2)) # Using set() + issubset() # Check if one dictionary is subset of other res = set (test_dict2.items()).issubset( set (test_dict1.items())) # printing result print ( "Does dict2 lie in dict1 ? : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The original dictionary 1 : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5} The original dictionary 2 : {'gfg': 1, 'is': 2, 'best': 3} Does dict2 lie in dict1 ? : True
Time complexity: O(n)
Auxiliary Space: O(1)
Please Login to comment...