Python - Nested dictionary Combinations


In the given problem statement we have to showcase the nested dictionary and its combinations using Python. So we will discuss some examples and understand what exactly a nested dictionary is, how it works and how we can use it.

What is a Dictionary in Python?

As you may probably know, the dictionary is a set of information in which data is stored in key-value pairs. If we take an example of an English dictionary, there are words which are the key and its value is the meaning of that particular word. Similarly Python provides some predefined data structures and dictionary is one of them which is the set of key-value pairs. Dictionaries are a great way to show structured data. The syntax for dictionary is −

Syntax

dictionary = {
   'key1': 'value1',
   'key2': 'value2',
   'key3': 'value3',
   'key4': 'value4'
   .
   .
   .
}

What is a Nested Dictionary in Python?

As we have seen what a dictionary is now we will understand what a nested dictionary is. The dictionary inside the dictionary is called a nested dictionary. Means a dictionary can contain another dictionary. This works like a hierarchy. The nested dictionary can also be created like the normal dictionary is created. So let’s see the example below −

nested_dictionary = {
   'key1': {
      'subkey1':
      'value1',
   },
   'key2':{
      'subkey2':
      'value2'
   },
}

Understanding the Logic for the Problem

The problem at hand is to create a nested dictionary combination and create the program in Python. For generating the combinations of nested dictionaries we will have to iterate the input dictionaries and its key-value pair. And then we will combine the dictionaries.

Algorithm

  • Step 1 − First import the product function from itertools library of Python. This function will be used to get all the possible combinations of the values.

  • Step 2 − Next, define the function called combine_dicts and pass a parameter in it as nested_dict.

  • Step 3 − Now initialize the blank dictionary to store the resultant dictionary of nested dictionary combinations.

  • Step 4 − In this step we will traverse the items of the dictionary to find the possible combinations.

  • Step 5 − Generate all the probable combinations of the dictionary. And set out the combinations and assign indexes to each combination.

  • Step 6 − Now we will create a combined dictionary with the help of keys and the current combinations.

  • Step 7 − At the end return the combined dictionary and call the function to show the result.

Example

# Import the necessary library
from itertools import product

# Define the function to combine the nested lists
def combine_dicts(nested_dict):
   # initialize the empty dictionary
   combined_dict = {}
   # Iterate the dictionary items
   for key, val in nested_dict.items():
      combinations = product(*val.values())
      for j, k in enumerate(combinations):
         combined_dict[key + str(j)] = dict(zip(val.keys(), k))

   return combined_dict

# Initialize the input dictionary
nested_dict = {'A': {'X' : [12, 15], 'Y': [31, 45]}}
print("The input dictionary : " + str(nested_dict))

# call the function
result = combine_dicts(nested_dict)
print("The combinations are : " + str(result))

Output

The input dictionary : {'A': {'X': [12, 15], 'Y': [31, 45]}}
The combinations are : {'A0': {'X': 12, 'Y': 31}, 'A1': {'X': 12, 'Y': 45}, 'A2': {'X': 15, 'Y': 31}, 'A3': {'X': 15, 'Y': 45}}

Complexity

The time complexity for combining the nested dictionary depends on some factors in the code. So the approximate time complexity for Iterating the nested dictionary takes O(n) time, here n is the number of key-value pairs. And to create possible combinations of nested dictionary takes O(n*m) time, here m is the number of keys in the new dictionary.

Conclusion

In this article we have seen a dictionary, a nested dictionary and how to combine the nested dictionary using Python. We have used the product function of the itertools library of Python. So this is the way we can create nested dictionary combinations using Python.

Updated on: 17-Oct-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements