Python – Nested Dictionary Subset
Last Updated :
10 Apr, 2023
Given a Nested Dictionary, test if another dictionary is a subset.
Examples:
Input : test_dict = {"gfg": 12, 'best' : {1 : 3, 4 : 3, 'geeks' : {8 : 7}}}, sub_dict = {8 : 7}
Output : True
Explanation : Required Nested dictionary present in Dictionary.
Input : test_dict = {"gfg": 12, 'best' : {1 : 3, 4 : 3, 'geeks' : {8 : 7}}}, sub_dict = {9 : 7}
Output : False
Explanation : Nested dictionary not present in Dictionary.
In this, We check for subset at each nesting using function, and check for all the keys matching using all(), any() is used for the utility to test for any nested possible subset matching the tested subset on the test dictionary. Each nesting is tested using recursion.
Python3
def check_eq(mast_dict, subdict):
if not isinstance (mast_dict, ( dict , list )):
return mast_dict = = subdict
if isinstance (mast_dict, list ):
return all (check_eq(x, y) for x, y in zip (mast_dict, subdict))
return all (mast_dict.get(idx) = = subdict[idx] or check_eq(mast_dict.get(idx), subdict[idx]) for idx in subdict)
def is_subset(mast_dict, subdict):
if isinstance (mast_dict, list ):
return any (is_subset(idx, subdict) for idx in mast_dict)
return check_eq(mast_dict, subdict) or ( isinstance (mast_dict, dict ) and any (is_subset(y, subdict) for y in mast_dict.values()))
test_dict = { "gfg" : 12 , 'best' : { 1 : 3 , 4 : 3 , 'geeks' : { 8 : 7 }}, 'cs' : 7 }
print ( "The original dictionary is : " + str (test_dict))
sub_dict = { 8 : 7 }
res = is_subset(test_dict, sub_dict)
print ( "Is dictionary subset : " + str (res))
|
Output:
The original dictionary is : {‘gfg’: 12, ‘best’: {1: 3, 4: 3, ‘geeks’: {8: 7}}, ‘cs’: 7} Is dictionary subset : True
Time complexity: O(N), where N is the number of elements in the master dictionary.
Auxiliary space: O(N), as it creates multiple recursive function calls and temporary dictionaries and lists during the search.
Method 2: Using set() method
Another approach to solving this problem is by converting the nested dictionaries into sets of tuples and then checking if the subset is present in the master dictionary using the set.issubset() method.
Step-by-step approach ;
- Define a function nested_dict_to_set(d) that takes a dictionary d as input.
- Check if the input dictionary d is an instance of the dictionary type using the isinstance() function.
- If d is an instance of dictionary, iterate over the key-value pairs in d using the items() method.
- For each key-value pair in d, apply the nested_dict_to_set() function recursively to the value, and create a tuple containing the key and the result of the recursive call.
- If d is an instance of a list, apply the nested_dict_to_set() function recursively to each item in the list using a list comprehension.
- If d is neither a dictionary nor a list, return d as is.
- Define a function is_subset(mast_dict, subdict) that takes two dictionaries mast_dict and subdict as input.
Inside the is_subset() function, call the nested_dict_to_set() function on both mast_dict and subdict to convert them into sets of tuples.
- Use the all() function to check if all the items in the subdict set are present in the mast_dict set.
- Return the result of the all() function.
- Initialize a dictionary test_dict with some nested dictionaries and values.
- Print the original dictionary test_dict.
- Initialize a subset dictionary sub_dict.
- Call the is_subset() function with test_dict and sub_dict as arguments and store the result in the res variable.
- Print the result of the is_subset() function call.
Python3
def nested_dict_to_set(d):
if isinstance (d, dict ):
return [(k, nested_dict_to_set(v)) for k, v in d.items()]
elif isinstance (d, list ):
return [nested_dict_to_set(item) for item in d]
else :
return d
def is_subset(mast_dict, subdict):
return all (item in nested_dict_to_set(mast_dict) for item in nested_dict_to_set(subdict))
test_dict = { "gfg" : 12 , 'best' : { 1 : 3 , 4 : 3 , 'geeks' : { 8 : 7 }}, 'cs' : 7 }
print ( "The original dictionary is : " + str (test_dict))
sub_dict = { 8 : 7 }
res = is_subset(test_dict, sub_dict)
print ( "Is dictionary subset : " + str (res))
|
Output
The original dictionary is : {'gfg': 12, 'best': {1: 3, 4: 3, 'geeks': {8: 7}}, 'cs': 7}
Is dictionary subset : False
Time complexity: O(n), where n is the total number of elements in the input dictionaries.
Auxiliary space: O(n), where n is the total number of elements in the input dictionary.
Method 3: Naive (using recursion and iteration in a single function)
Here we will be checking if the sub_dict is a subset of the master_dict.
Approach:
- Create a function called is_subset_dict that takes two arguments master_dict and sub_dict.
- Check if the sub_dict is empty, if it is empty, then return True because an empty dictionary is always a subset of any dictionary.
- Iterate over the keys and values of the sub_dict, and for each key-value pair:
- a. Check if the key exists in the master_dict.
b. If the key exists, check if the value is a dictionary or not. If it is a dictionary, then call the is_subset_dict function recursively with the corresponding sub-dictionaries.
c. If the value is not a dictionary, compare the values of the sub_dict and master_dict.
d. If the value is not present in the master_dict, then return False.
e. If all the values in the sub_dict are present in the master_dict, then return True.
Python3
def is_subset_dict(dict1, dict2):
dict1_keys = set (dict1.keys())
dict2_keys = set (dict2.keys())
intersection = dict1_keys.intersection(dict2_keys)
if intersection = = dict2_keys:
for key in dict2_keys:
if dict1[key] ! = dict2[key]:
return False
return True
else :
return False
dict1 = { 'a' : 1 , 'b' : 2 , 'c' : 3 , 'd' : 4 }
dict2 = { 'b' : 2 , 'd' : 4 }
dict3 = { 'b' : 2 , 'd' : 5 }
print (is_subset_dict(dict1, dict2))
print (is_subset_dict(dict1, dict3))
|
Time complexity: O(N*M) where N is the number of keys in the sub_dict and M is the maximum depth of the sub_dict.
Auxiliary space complexity is O(M) where M is the maximum depth of the sub_dict.
Method 4: Using the all() function and dictionary comprehension
The all() function returns True if all elements of an iterable are true. We can use this function with a dictionary comprehension to check if all key-value pairs in dict2 are also present in dict1.
Approach :
- The function takes two dictionaries, dict1 and dict2, as inputs.
- The function creates a boolean variable is_subset and sets it to True.
- The function creates a dictionary comprehension that iterates over the key-value pairs in dict2. For each key-value pair (k, v) in dict2, the comprehension checks if k is a key in dict1 and if v is equal to the value of dict1[k].
- The function passes the resulting dictionary comprehension to the all() function, which returns True if all key-value pairs in dict2 are also present in dict1.
- If the all() function returns False, the function sets is_subset to False.
- The function returns the value of is_subset.
Python3
def is_subset_dict(dict1, dict2):
is_subset = all (k in dict1 and dict1[k] = = v for k, v in dict2.items())
return is_subset
dict1 = { 'a' : 1 , 'b' : 2 , 'c' : 3 , 'd' : 4 }
dict2 = { 'b' : 2 , 'd' : 4 }
dict3 = { 'b' : 2 , 'd' : 5 }
print (is_subset_dict(dict1, dict2))
print (is_subset_dict(dict1, dict3))
|
Time complexity: O(len(dict2)), as the dictionary comprehension and all() function iterate over the key-value pairs in dict2.
Auxiliary space: O(1), as we only create a single boolean variable is_subset.
Method 5 : use the built-in function issuperset().
Step-by-step approach:
Step 1: Create two sets using the items() method on the dictionaries.
Step 2: Use the issuperset() function to check if the first set is a superset of the second set.
Step 3: Return True if the first set is a superset of the second set, False otherwise.
Python3
def is_subset_dict(dict1, dict2):
set1 = set (dict1.items())
set2 = set (dict2.items())
return set1.issuperset(set2)
dict1 = { 'a' : 1 , 'b' : 2 , 'c' : 3 , 'd' : 4 }
dict2 = { 'b' : 2 , 'd' : 4 }
dict3 = { 'b' : 2 , 'd' : 5 }
print (is_subset_dict(dict1, dict2))
print (is_subset_dict(dict1, dict3))
|
Time complexity: O(n), where n is the number of elements in the larger dictionary.
Auxiliary space: O(n)
Similar Reads
Three Level Nested Dictionary Python
In Python, a dictionary is a built-in data type used to store data in key-value pairs. Defined with curly braces `{}`, each pair is separated by a colon `:`. This allows for efficient representation and easy access to data, making it a versatile tool for organizing information. What is 3 Level Neste
4 min read
Python - Sorted Nested Keys in Dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract all the keys of nested dictionaries and render them in sorted order. This kind of application can occur in domains in which we work with data. Lets discuss certain ways in which this task can be perf
4 min read
Python - Resize Keys in dictionary
Given Dictionary, resize keys to K by getting starting k elements from keys. Input : test_dict = {"geeksforgeeks" :3, "best" :3, "coding" :4, "practice" :3}, K = 3 Output : {'gee': 3, 'bes': 3, 'cod': 4, 'pra': 3} Explanation : Keys resized to have 3 elements. Input : test_dict = {"geeksforgeeks" :3
3 min read
Python | Safe access nested dictionary keys
Sometimes, while working with Python we can have a problem in which we need to get the 2nd degree key of dictionary i.e the nested key. This type of problem is common in case of web development, especially with the advent of NoSQL databases. Let's discuss certain ways to safely get the nested availa
3 min read
Inverse Dictionary Values List - Python
We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]} Using defau
2 min read
Get Dictionary Value by Key - Python
We are given a dictionary and our task is to retrieve the value associated with a given key. However, if the key is not present in the dictionary we need to handle this gracefully to avoid errors. For example, consider the dictionary : d = {'name': 'Alice', 'age': 25, 'city': 'New York'} if we try t
3 min read
Parsing Json Nested Dictionary Using Python
We are given a JSON string and we have to parse a nested dictionary from it using different approaches in Python. In this article, we will see how we can parse nested dictionary from a JSON object using Python. Example: Input: json_data = '{"name": "John", "age": 30, "address": {"city": "New York",
3 min read
Python Remove Dictionary Item
Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods. Using po
3 min read
Python Iterate Dictionary Key, Value
In Python, a Dictionary is a data structure that stores the data in the form of key-value pairs. It is a mutable (which means once created we modify or update its value later on) and unordered data structure in Python. There is a thing to keep in mind while creating a dictionary every key in the dic
3 min read
Check if one dictionary is subset of other - Python
Checking if one dictionary is a subset of another involves verifying whether all key-value pairs of the smaller dictionary exist in the larger dictionary with the same values. For example, given two dictionaries a = {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5} and b = {'gfg': 1, 'is': 2, 'best'
4 min read