Open In App

Convert Lists to Nested Dictionary – Python

Last Updated : 12 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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