Convert Lists to Nested Dictionary – Python
Last Updated :
12 Feb, 2025
The task of converting lists to a nested dictionary in Python involves mapping elements from multiple lists into key-value pairs, where each key is associated with a nested dictionary. For example, given the lists a = [“gfg”, “is”, “best”], b = [“ratings”, “price”, “score”], and c = [5, 6, 7], the goal is to create a dictionary like {‘gfg’: {‘ratings’: 5}, ‘is’: {‘price’: 6}, ‘best’: {‘score’: 7}} .
Using dictionary comprehension
Dictionary Comprehension is a efficient method to transform multiple lists into a nested dictionary. It’s often the most preferred technique because it allows us to create a dictionary in a single, readable line. This method combines both looping and dictionary creation in one clean structure, which makes it highly efficient for converting lists into a nested dictionary.
Python
a = ["gfg", 'is', 'best']
b = ['ratings', 'price', 'score']
c = [5, 6, 7]
res = {i: {j: k} for i, j, k in zip(a, b, c)}
print(res)
Output{'gfg': {'ratings': 5}, 'is': {'price': 6}, 'best': {'score': 7}}
Explanation: list comprehension zips lists a, b, and c, then creates a nested dictionary where each key from a maps to a dictionary with keys from b and values from c.
Using dict()
dict() can be used in combination with map() or lambda to create a nested dictionary. By using map(), we can apply a transformation to each element from the combined lists, making this method a functional programming alternative to dictionary comprehension.
Python
a = ["gfg", 'is', 'best']
b = ['ratings', 'price', 'score']
c = [5, 6, 7]
res = dict(map(lambda x: (x[0], {x[1]: x[2]}), zip(a, b, c)))
print(res)
Output{'gfg': {'ratings': 5}, 'is': {'price': 6}, 'best': {'score': 7}}
Explanation: map() zipped elements from a, b, and c into key-value pairs, where each key from a links to a nested dictionary from b and c and dict() converts it into a nested dictionary.
Using collections.defaultdict
defaultdict from the collections module is a flexible approach that automatically initializes dictionary entries. While it’s not as efficient as dictionary comprehension in this case, it provides additional flexibility, especially when dealing with more complex nested structures.
Python
from collections import defaultdict
a = ["gfg", 'is', 'best']
b = ['ratings', 'price', 'score']
c = [5, 6, 7]
res = defaultdict(dict)
for i, j, k in zip(a, b, c):
res[i][j] = k
print(dict(res))
Output{'gfg': {'ratings': 5}, 'is': {'price': 6}, 'best': {'score': 7}}
Explanation: for loop iterates over tuples from zip(a, b, c), assigning k as the value to key j within the nested dictionary of key i. Finally, dict(res) converts the defaultdict to a regular dictionary.
Using loop
Loop provides explicit control over the process of creating a nested dictionary. While this method is simple to understand , it’s less concise than the other methods and introduces additional lines of code. This approach may be suitable when we need more custom logic or when we’re building dictionaries incrementally.
Python
a = ["gfg", 'is', 'best']
b = ['ratings', 'price', 'score']
c = [5, 6, 7]
res = {}
for i, j, k in zip(a, b, c):
res[i] = {j: k}
print(res)
Output{'gfg': {'ratings': 5}, 'is': {'price': 6}, 'best': {'score': 7}}
Explanation: for loop iterates over zip(a, b, c) assigning {j: k} as a nested dictionary to the key i in res during each iteration.
Similar Reads
Python - Convert Lists to Nested Dictionary
The task of converting lists to a nested dictionary in Python involves mapping elements from multiple lists into key-value pairs, where each key is associated with a nested dictionary. For example, given the lists a = ["gfg", "is", "best"], b = ["ratings", "price", "score"], and c = [5, 6, 7], the g
3 min read
Convert nested Python dictionary to object
Let us see how to convert a given nested dictionary into an object Method 1 : Using the json module. We can solve this particular problem by importing the json module and use a custom object hook in the json.loads() method. C/C++ Code # importing the module import json # declaringa a class class obj
2 min read
Convert Two Lists into a Dictionary - Python
We are given two lists, we need to convert both of the list into dictionary. For example we are given two lists a = ["name", "age", "city"], b = ["Geeks", 30,"Delhi"], we need to convert these two list into a form of dictionary so that the output should be like {'name': 'Geeks', 'age': 30, 'city': '
3 min read
Python - Convert list of dictionaries to JSON
In this article, we will discuss how to convert a list of dictionaries to JSON in Python. Python Convert List of Dictionaries to JsonBelow are the ways by which we can convert a list of dictionaries to JSON in Python: Using json.dumps()Using json.dump()Using json.JSONEncoderUsing default ParameterDi
5 min read
Python - Converting list string to dictionary
Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val
3 min read
Python - Convert Dictionary Object into String
In Python, there are situations where we need to convert a dictionary into a string format. For example, given the dictionary {'a' : 1, 'b' : 2} the objective is to convert it into a string like "{'a' : 1, 'b' : 2}". Let's discuss different methods to achieve this: Using strThe simplest way to conve
2 min read
Python | Convert list of tuple into dictionary
Given a list containing all the element and second list of tuple depicting the relation between indices, the task is to output a dictionary showing the relation of every element from the first list to every other element in the list. These type of problems are often encountered in Coding competition
8 min read
How to convert a MultiDict to nested dictionary using Python
A MultiDict is a dictionary-like object that holds multiple values for the same key, making it a useful data structure for processing forms and query strings. It is a subclass of the Python built-in dictionary and behaves similarly. In some use cases, we may need to convert a MultiDict to a nested d
3 min read
How To Convert Python Dictionary To JSON?
In Python, a dictionary stores information using key-value pairs. But if we want to save this data to a file, share it with others, or send it over the internet then we need to convert it into a format that computers can easily understand. JSON (JavaScript Object Notation) is a simple format used fo
7 min read
Convert a list of Tuples into Dictionary - Python
Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p
3 min read