Open In App

Convert Value List Elements to List Records – Python

Last Updated : 21 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a dictionary with lists as values and the task is to transform each element in these lists into individual dictionary records. Specifically, each list element should become a key in a new dictionary with an empty list as its value.
For example, given {‘gfg’: [4, 5], ‘best’: [8, 10, 7, 9]}, the output should be {‘gfg’: [{4: []}, {5: []}], ‘best’: [{8: []}, {10: []}, {7: []}, {9: []}]}.

Using enumerate()

This brute-force method iterates through all values (lists) in the dictionary and for each element in the list it creates a new dictionary with the element as the key and an empty list as its value. The enumerate() function is used to track the index while iterating allowing us to replace each element directly.

Python
d = {'gfg': [4, 5], 'is': [8], 'best': [10]}

# Transforming list elements into dictionaries
for li in d.values():
    for i, v in enumerate(li):
        li[i] = {v: []}

print(d)

Output
{'gfg': [{4: []}, {5: []}], 'is': [{8: []}], 'best': [{10: []}]}

Explanation:

  • Outer loop iterates over all lists in the dictionary’s values.
  • Inner loop replaces each list element with a dictionary {element: []} using its index (i).

Using dictionary comprehension

In this method we utilize dictionary comprehension to create a new dictionary where the items() function is used to iterate over key-value pairs of the original dictionary. For each list in the values, a nested list comprehension converts its elements into dictionaries {element: []}.

Python
d = {'gfg': [4, 5], 'is': [8], 'best': [10]}

# Transforming list elements into dictionaries
res = {k: [{v: []} for v in li] for k, li in d.items()}

print(res)

Output
{'gfg': [{4: []}, {5: []}], 'is': [{8: []}], 'best': [{10: []}]}

Explanation:

  • Outer dictionary comprehension iterates over key-value pairs (k, li) using items().
  • Inner list comprehension converts each element (v) in the list (li) into a dictionary {v: []}.

Using Recursive Method

We can solve the given problem with recursion by traversing the dictionary and checking if the value is a list, then convert each element into a dictionary with the element as the key and an empty list as its value but if the value is a dictionary then recursively process it.

Python
# Recursive function to convert list elements to dictionaries
def convert_to_list(d):
    for k in d:
        if isinstance(d[k], list):
            d[k] = [{v: []} if not isinstance(v, dict) else v for v in d[k]]
        elif isinstance(d[k], dict):
            convert_to_list_records(d[k])
    return d

d = {'gfg': [4, 5], 'is': [8], 'best': [10]}
res = convert_to_list_records(d)
print(res)

Output
{'gfg': [{4: []}, {5: []}], 'is': [{8: []}], 'best': [{10: []}]}

Explanation:

  • Function convert_to_list iterates through the dictionary, If a value is a list then it converts each element into a dictionary with an empty list and if the value is a dictionary then it calls itself to process that dictionary.
  • When the value is a list then each element is converted to a dictionary {v: []} if it’s not already a dictionary.


Next Article
Practice Tags :

Similar Reads