Python – Convert Flat dictionaries to Nested dictionary
Sometimes, while working with records, we can have a problem in which we need to perform the task of conversion of multiple flat dictionaries to a single nested dictionary. This can have applications in many domains in which data is used extensively. Let’s discuss certain ways by which we can convert flat dictionaries into nested dictionaries.
Convert Flat Dictionaries to Nested dictionary in Python
Below are the ways by which we can convert flat dictionaries to nested dictionary in Python:
- Using dict() + key access
- Using zip
- Using a for loop and dictionary comprehension
- Using defaultdict() and reduce()
Convert Flattened Dictionaries to Nested dictionary Using dict() + key access
This is one of the way in which this task can be performed. In this, we construct empty dictionary using dict() and assign a new level to dictionary using manual brute key access.
Python3
# initializing dictionaries test_dict1 = { 'gfg' : 1 , 'best' : 2 } test_dict2 = { 'for' : 3 , 'Beginner' : 5 } # printing original dictionaries print ( "The original dictionary 1 is : " + str (test_dict1)) print ( "The original dictionary 2 is : " + str (test_dict2)) # Convert Flat dictionaries to Nested dictionary # Using key access + dict() res = dict () res[ "level1" ] = test_dict1 res[ 'level2' ] = test_dict2 # printing result print ( "The nested dictionary is : " + str (res)) |
Output:
The original dictionary 1 is : {'gfg': 1, 'best': 2}
The original dictionary 2 is : {'for': 3, 'Beginner': 5}
The nested dictionary is : {'level1': {'gfg': 1, 'best': 2}, 'level2': {'for': 3, 'Beginner': 5}}
Time complexity: O(1)
Auxiliary Space: O(1)
Python Convert Flat Dictionaries to Nested dictionary Using zip()
This is another way in which this task can be performed. In this we link inner keys to outer keys using zip().
Python3
# initializing dictionaries test_dict1 = { 'gfg' : 1 , 'best' : 2 } test_dict2 = { 'for' : 3 , 'Beginner' : 5 } # printing original dictionaries print ( "The original dictionary 1 is : " + str (test_dict1)) print ( "The original dictionary 2 is : " + str (test_dict2)) # Convert Flat dictionaries to Nested dictionary # Using zip() key_dict = [ 'level1' , 'level2' ] dict_list = [test_dict1, test_dict2] res = dict ( zip (key_dict, dict_list)) # printing result print ( "The nested dictionary is : " + str (res)) |
Output:
The original dictionary 1 is : {'gfg': 1, 'best': 2}
The original dictionary 2 is : {'for': 3, 'Beginner': 5}
The nested dictionary is : {'level1': {'gfg': 1, 'best': 2}, 'level2': {'for': 3, 'Beginner': 5}}
Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary Space: O(1), constant extra space is required
Convert Flat Dictionaries to Nested dictionary Using a for loop and dictionary comprehension
In this example, flat dictionaries test_dict1
and test_dict2
are transformed into a nested dictionary res
using a for loop and dictionary comprehension, associating each original dictionary with its corresponding key in the list key_dict
.
Python3
# initializing dictionaries test_dict1 = { 'gfg' : 1 , 'best' : 2 } test_dict2 = { 'for' : 3 , 'Beginner' : 5 } # printing original dictionaries print ( "The original dictionary 1 is : " + str (test_dict1)) print ( "The original dictionary 2 is : " + str (test_dict2)) # Convert Flat dictionaries to Nested dictionary # Using a for loop and dictionary comprehension key_dict = [ 'level1' , 'level2' ] dict_list = [test_dict1, test_dict2] res = {} for key, d in zip (key_dict, dict_list): res[key] = {k: v for k, v in d.items()} # printing result print ( "The nested dictionary is : " + str (res)) |
Output:
The original dictionary 1 is : {'gfg': 1, 'best': 2}
The original dictionary 2 is : {'for': 3, 'Beginner': 5}
The nested dictionary is : {'level1': {'gfg': 1, 'best': 2}, 'level2': {'for': 3, 'Beginner': 5}}
Time Complexity: O(n), where n is the number of items in the dictionaries.
Auxiliary Space: O(n)
Python Convert Flat Dictionaries to Nested dictionary Using defaultdict() and reduce() Functions
In this example, flat dictionaries test_dict1
and test_dict2
are transformed into a nested dictionary (nested_dict
) using defaultdict
, functools.reduce
, and operator.setitem
to iteratively update the nested structure with key-value pairs.
Python3
from collections import defaultdict from functools import reduce import operator # initializing dictionaries test_dict1 = { 'gfg' : 1 , 'best' : 2 } test_dict2 = { 'for' : 3 , 'Beginner' : 5 } # printing original dictionaries print ( "The original dictionary 1 is : " + str (test_dict1)) print ( "The original dictionary 2 is : " + str (test_dict2)) # Convert Flat dictionaries to Nested dictionary using defaultdict, functools, operators nested_dict = defaultdict( dict ) # Combine key-value pairs into a single list key_value_pairs = [( 'level1' , test_dict1), ( 'level2' , test_dict2)] # Use functools.reduce and operator.setitem to create the nested dictionary reduce ( lambda d, kv: operator.setitem( d, kv[ 0 ], kv[ 1 ]) or d, key_value_pairs, nested_dict) # Convert defaultdict to regular dictionary nested_dict = dict (nested_dict) # printing result print ( "The nested dictionary is : " + str (nested_dict)) |
Output:
The original dictionary 1 is : {'gfg': 1, 'best': 2}
The original dictionary 2 is : {'for': 3, 'Beginner': 5}
The nested dictionary is : {'level1': {'gfg': 1, 'best': 2}, 'level2': {'for': 3, 'Beginner': 5}}
Contact Us