Sometimes, while working with Python Dictonaries, we can have a problem in which we need to convert dictionary list to nested records dictionary taking each index of dictionary list value and flattening it. This kind of problem can have application in many domains. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [{‘Gfg’: [5, 6]}, {‘best’: [3, 1]}]
Output : [{‘Gfg’: 5, ‘best’: 3}, {‘Gfg’: 6, ‘best’: 1}]Input : test_list = [{‘Gfg’: [5]}]
Output : [{‘Gfg’: 5}]
Method #1 : Using loop
This is one of the ways in which this problem can be solved. In this, we perform the task in brute force manner iterating each list value index and designating a key to it.
# Python3 code to demonstrate working of # Convert Dictionary Value list to Dictionary List # Using loop # initializing list test_list = [{'Gfg' : [5, 6, 5]}, {'is' : [10, 2, 3]}, {'best' : [4, 3, 1]}] # printing original list print("The original list is : " + str(test_list)) # Convert Dictionary Value list to Dictionary List # Using loop res =[{} for idx in range(len(test_list))] idx = 0for sub in test_list: for key, val in sub.items(): for ele in val: res[idx][key] = ele idx += 1 idx = 0 # printing result print("Records after conversion : " + str(res)) |
The original list is : [{‘Gfg’: [5, 6, 5]}, {‘is’: [10, 2, 3]}, {‘best’: [4, 3, 1]}]
Records after conversion : [{‘Gfg’: 5, ‘is’: 10, ‘best’: 4}, {‘Gfg’: 6, ‘is’: 2, ‘best’: 3}, {‘Gfg’: 5, ‘is’: 3, ‘best’: 1}]
Method #2 : Using list comprehension + zip()
This is yet another way in which we can perform this task. In this, we perform task in 2 steps, first, we extract all the keys and then pair up values using zip() accordingly.
# Python3 code to demonstrate working of # Convert Dictionary Value list to Dictionary List # Using list comprehension + zip() # initializing list test_list = [{'Gfg' : [5, 6, 5]}, {'is' : [10, 2, 3]}, {'best' : [4, 3, 1]}] # printing original list print("The original list is : " + str(test_list)) # Convert Dictionary Value list to Dictionary List # Using list comprehension + zip() keys = [list(sub.keys())[0] for sub in test_list] vals = zip(*[val for sub in test_list for val in sub.values()]) res = [dict(zip(keys, val)) for val in vals] # printing result print("Records after conversion : " + str(res)) |
The original list is : [{‘Gfg’: [5, 6, 5]}, {‘is’: [10, 2, 3]}, {‘best’: [4, 3, 1]}]
Records after conversion : [{‘Gfg’: 5, ‘is’: 10, ‘best’: 4}, {‘Gfg’: 6, ‘is’: 2, ‘best’: 3}, {‘Gfg’: 5, ‘is’: 3, ‘best’: 1}]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Convert list to Single Dictionary Key Value list
- Python - Convert list of dictionaries to Dictionary Value list
- Python - Convert String List to Key-Value List dictionary
- Python | Convert list of tuples to dictionary value lists
- Python - Convert List to Index and Value dictionary
- Python - Convert tuple list to dictionary with key from a given start value
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python - Replace dictionary value from other dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert key-value pair comma separated string into dictionary
- Python - Convert key-value String to dictionary
- Python - Convert Strings to Uppercase in Dictionary value lists
- Python | Convert List of Dictionary to Tuple list
- Python - Extract ith Key's Value of K's Maximum value dictionary
- Python - Assigning Key values to list elements from Value list Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

