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
# initializing list test_list1 = [ "gfg" , 'is' , 'best' ] test_list2 = [ 'ratings' , 'price' , 'score' ] test_list3 = [ 5 , 6 , 7 ] # printing original list 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)) # Convert Lists to Nestings Dictionary # Using list comprehension + zip() res = [{a: {b: c}} for (a, b, c) in zip (test_list1, test_list2, test_list3)] # printing result print ( "The constructed dictionary : " + str (res)) |
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
# initializing lists test_list1 = [ "gfg" , 'is' , 'best' ] test_list2 = [ 'ratings' , 'price' , 'score' ] test_list3 = [ 5 , 6 , 7 ] # initializing dictionary result_dict = {} # printing original list 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)) # iterating through the lists for i in range ( len (test_list1)): sub_dict = {test_list2[i]: test_list3[i]} result_dict[test_list1[i]] = sub_dict # printing result print ( "The constructed dictionary : " + str (result_dict)) |
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
# initializing lists test_list1 = [ "gfg" , 'is' , 'best' ] test_list2 = [ 'ratings' , 'price' , 'score' ] test_list3 = [ 5 , 6 , 7 ] # using dictionary comprehension to create a nested dictionary res = {a: {b: c} for (a, b, c) in zip (test_list1, test_list2, test_list3)} # printing original list 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)) # printing the resulting dictionary print ( "The constructed dictionary: " , res) |
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 # Initializing lists test_list1 = [ "gfg" , 'is' , 'best' ] test_list2 = [ 'ratings' , 'price' , 'score' ] test_list3 = [ 5 , 6 , 7 ] # Creating an iterator that cycles through the lists cycle_lists = itertools.cycle([test_list1, test_list2, test_list3]) # Initializing an empty dictionary res = {} # printing original list 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)) # Iterating over the combined elements of the lists for a, b, c in zip ( next (cycle_lists), next (cycle_lists), next (cycle_lists)): res[a] = {b: c} # Printing the resulting dictionary print ( "The constructed dictionary: " , res) |
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)
Python – Convert Lists to Nested Dictionary
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.