Python – Convert Lists to Nested Dictionary
Last Updated :
05 Dec, 2023
Sometimes, while working with Python dictionaries, we can have a problem in which we need to convert lists to nestings, i.e. each list value represents a new nested level. This kind of problem can have applications in many domains including web development. Let’s discuss the certain way in which this task can be performed.
Convert Lists to Nested Dictionary in Python
Below are the ways by which we can convert lists to nested dictionary in Python:
- Using zip() and list comprehension
- Naive (Using for loop)
- Using dictionary comprehension
- Using the Itertools module
Convert Lists to Nested Dictionary Using zip() + list comprehension
The combination of the above functions can be combined to perform this task. In this, we iterate for a zipped list and render the nested dictionaries using list comprehension. Each element at corresponding positions in the three lists contributes to a key-value pair in the resulting dictionary, where the first list provides the outermost keys, the second list provides the intermediate keys, and the third list provides the values.
Python3
test_list1 = [ "gfg" , 'is' , 'best' ]
test_list2 = [ 'ratings' , 'price' , 'score' ]
test_list3 = [ 5 , 6 , 7 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The original list 3 is : " + str (test_list3))
res = [{a: {b: c}} for (a, b, c) in zip (test_list1, test_list2, test_list3)]
print ( "The constructed dictionary : " + str (res))
|
Output
The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : ['ratings', 'price', 'score']
The original list 3 is : [5, 6, 7]
The constructed dictionary : [{'gfg': {'ratings': 5}}, {'is': {...
Time complexity: O(n)
Auxiliary space: O(n)
Convert Lists to Nested Dictionary in Python Using for loop
In this example, Python code utilizes a for loop to convert three separate lists (test_list1
, test_list2
, and test_list3
) into a nested dictionary (res
). Each element at corresponding positions in the three lists contributes to a key-value pair in the resulting dictionary, where the elements from test_list1
serve as outermost keys, elements from test_list2
serve as intermediate keys, and elements from test_list3
serve as values.
Python3
test_list1 = [ "gfg" , 'is' , 'best' ]
test_list2 = [ 'ratings' , 'price' , 'score' ]
test_list3 = [ 5 , 6 , 7 ]
result_dict = {}
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The original list 3 is : " + str (test_list3))
for i in range ( len (test_list1)):
sub_dict = {test_list2[i]: test_list3[i]}
result_dict[test_list1[i]] = sub_dict
print ( "The constructed dictionary : " + str (result_dict))
|
Output
The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : ['ratings', 'price', 'score']
The original list 3 is : [5, 6, 7]
The constructed dictionary : {'gfg': {'ratings': 5}, 'is': {'pr...
Time Complexity: O(n), where n is the length of the lists
Auxiliary Space: O(n), where n is the length of the lists
Python Convert Lists to Nested Dictionary Using Dictionary Comprehension
In this example, the Python code utilizes dictionary comprehension and the zip()
function to create a nested dictionary (res
) from three lists (test_list1
, test_list2
, and test_list3
). Each element at corresponding positions in the three lists contributes to a key-value pair in the resulting dictionary, where the elements from test_list1
serve as outermost keys, elements from test_list2
serve as intermediate keys, and elements from test_list3
serve as values.
Python3
test_list1 = [ "gfg" , 'is' , 'best' ]
test_list2 = [ 'ratings' , 'price' , 'score' ]
test_list3 = [ 5 , 6 , 7 ]
res = {a: {b: c} for (a, b, c) in zip (test_list1, test_list2, test_list3)}
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The original list 3 is : " + str (test_list3))
print ( "The constructed dictionary: " , res)
|
Output
The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : ['ratings', 'price', 'score']
The original list 3 is : [5, 6, 7]
The constructed dictionary: {'gfg': {'ratings': 5}, 'is': {'pr...
Time Complexity: O(n)
Auxiliary Space: O(n)
Convert Lists to Nested Dictionary in Python Using the itertools module
In this example, Python code utilizes the itertools.cycle()
function to create an iterator that cycles through the provided lists (test_list1
, test_list2
, and test_list3
). It then iterates over the combined elements of the lists using the zip()
function, creating a nested dictionary (res
) where elements from test_list1
serve as outermost keys, elements from test_list2
serve as intermediate keys, and elements from test_list3
serve as values.
Python3
import itertools
test_list1 = [ "gfg" , 'is' , 'best' ]
test_list2 = [ 'ratings' , 'price' , 'score' ]
test_list3 = [ 5 , 6 , 7 ]
cycle_lists = itertools.cycle([test_list1, test_list2, test_list3])
res = {}
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The original list 3 is : " + str (test_list3))
for a, b, c in zip ( next (cycle_lists), next (cycle_lists), next (cycle_lists)):
res[a] = {b: c}
print ( "The constructed dictionary: " , res)
|
Output
The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : ['ratings', 'price', 'score']
The original list 3 is : [5, 6, 7]
The constructed dictionary: {'gfg': {'ratings': 5}, 'is': {'pr...
Time Complexity: O(n)
Auxiliary Space: O(1)