Python – Convert Flat dictionaries to Nested dictionary
Last Updated :
05 Dec, 2023
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
test_dict1 = { 'gfg' : 1 , 'best' : 2 }
test_dict2 = { 'for' : 3 , 'geeks' : 5 }
print ( "The original dictionary 1 is : " + str (test_dict1))
print ( "The original dictionary 2 is : " + str (test_dict2))
res = dict ()
res[ "level1" ] = test_dict1
res[ 'level2' ] = test_dict2
print ( "The nested dictionary is : " + str (res))
|
Output:
The original dictionary 1 is : {'gfg': 1, 'best': 2}
The original dictionary 2 is : {'for': 3, 'geeks': 5}
The nested dictionary is : {'level1': {'gfg': 1, 'best': 2}, 'level2': {'for': 3, 'geeks': 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
test_dict1 = { 'gfg' : 1 , 'best' : 2 }
test_dict2 = { 'for' : 3 , 'geeks' : 5 }
print ( "The original dictionary 1 is : " + str (test_dict1))
print ( "The original dictionary 2 is : " + str (test_dict2))
key_dict = [ 'level1' , 'level2' ]
dict_list = [test_dict1, test_dict2]
res = dict ( zip (key_dict, dict_list))
print ( "The nested dictionary is : " + str (res))
|
Output:
The original dictionary 1 is : {'gfg': 1, 'best': 2}
The original dictionary 2 is : {'for': 3, 'geeks': 5}
The nested dictionary is : {'level1': {'gfg': 1, 'best': 2}, 'level2': {'for': 3, 'geeks': 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
test_dict1 = { 'gfg' : 1 , 'best' : 2 }
test_dict2 = { 'for' : 3 , 'geeks' : 5 }
print ( "The original dictionary 1 is : " + str (test_dict1))
print ( "The original dictionary 2 is : " + str (test_dict2))
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()}
print ( "The nested dictionary is : " + str (res))
|
Output:
The original dictionary 1 is : {'gfg': 1, 'best': 2}
The original dictionary 2 is : {'for': 3, 'geeks': 5}
The nested dictionary is : {'level1': {'gfg': 1, 'best': 2}, 'level2': {'for': 3, 'geeks': 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
test_dict1 = { 'gfg' : 1 , 'best' : 2 }
test_dict2 = { 'for' : 3 , 'geeks' : 5 }
print ( "The original dictionary 1 is : " + str (test_dict1))
print ( "The original dictionary 2 is : " + str (test_dict2))
nested_dict = defaultdict( dict )
key_value_pairs = [( 'level1' , test_dict1), ( 'level2' , test_dict2)]
reduce ( lambda d, kv: operator.setitem(
d, kv[ 0 ], kv[ 1 ]) or d, key_value_pairs, nested_dict)
nested_dict = dict (nested_dict)
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, 'geeks': 5}
The nested dictionary is : {'level1': {'gfg': 1, 'best': 2}, 'level2': {'for': 3, 'geeks': 5}}
Share your thoughts in the comments
Please Login to comment...