Open In App

Convert Dictionary of Dictionaries to Python List of Dictionaries

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Dictionaries are powerful data structures in Python, allowing the storage of key-value pairs. Sometimes, we encounter scenarios where we have a dictionary of dictionaries, and we need to convert it into a list of dictionaries for easier manipulation or processing. In this article, we’ll explore five different methods to achieve this conversion, each with its advantages and use cases.

Converting A Dictionary Of Dictionaries To A List Of Dictionaries

Below, are the methods of Converting A Dictionary Of Dictionaries To A List Of Dictionaries in Python.

Create Dictionary

This dictionary represents information about three individuals, with each person having attributes like name, age, and city.

Python3




data = {
    'person1': {'name': 'Alice', 'age': 30, 'city': 'New York'},
    'person2': {'name': 'Bob', 'age': 25, 'city': 'San Francisco'},
    'person3': {'name': 'Charlie', 'age': 35, 'city': 'Los Angeles'}
}


Convert A Dictionary Of Dictionaries To A List Using List Comprehension

In this example, the below code initializes a list of dictionaries by using a list comprehension to extract key-value pairs from each inner dictionary within the original ‘data’ dictionary.

Python3




list_of_dicts = [{key: value for key, value in nested_dict.items()} for nested_dict in data.values()]
print(type(data))
print("Result:", list_of_dicts)
print(type(list_of_dicts))


Output

<class 'dict'>
Result: [{'name': 'Alice', 'age': 30, 'city': 'New York'},
{'name': 'Bob', 'age': 25, 'city': 'San Francisco'},
{'name': 'Charlie', 'age': 35, 'city': 'Los Angeles'}]
<class 'list'>

Convert A Dictionary Of Dictionaries To A List Using the `map` Function

In this example, in below code, the `map` function is employed to convert the values of the ‘data’ dictionary into individual dictionaries, resulting in a ‘list_of_dicts.’ The code then prints the type of the original ‘data’ dictionary, displays the result.

Python3




# Using the `map` Function
list_of_dicts = list(map(dict, data.values()))
 
print(type(data))
print(" Result:", list_of_dicts)
print(type(list_of_dicts))


Output

<class 'dict'>
Result: [{'name': 'Alice', 'age': 30, 'city': 'New York'},
{'name': 'Bob', 'age': 25, 'city': 'San Francisco'},
{'name': 'Charlie', 'age': 35, 'city': 'Los Angeles'}]
<class 'list'>

Convert A Dictionary Of Dictionaries To A List Using `copy` and `update`

In this example, iin below code a new list of dictionaries, ‘list_of_dicts,’ is created by iterating through the items of the original ‘data’ dictionary. For each inner dictionary, a copy is made, and the ‘id’ key-value pair is added using the `update` method.

Python3




#Using `copy` and `update` Methods
list_of_dicts = []
for key, value in data.items():
    temp_dict = value.copy()
    temp_dict.update({'id': key})
    list_of_dicts.append(temp_dict)
     
print(type(data))
print("Result:", list_of_dicts)
print(type(list_of_dicts))


Output

<class 'dict'>
Result: [{'name': 'Alice', 'age': 30, 'city': 'New York'},
{'name': 'Bob', 'age': 25, 'city': 'San Francisco'},
{'name': 'Charlie', 'age': 35, 'city': 'Los Angeles'}]
<class 'list'>

Convert A Dictionary Of Dictionaries To A List Using Enumerate() Function

In this example, in below code the `enumerate` function is used with a list comprehension to create a ‘list_of_dicts.’ Each inner dictionary is augmented with the additional key-value pair ‘id’: key, where the key is the enumeration index starting from 1.

Python3




# Using Enumerate
list_of_dicts = [{**value, 'id': key} for key, value in enumerate(data.values(), 1)]
 
print(type(data))
print("Result:", list_of_dicts)
print(type(list_of_dicts))


Output

<class 'dict'>
Result: [{'name': 'Alice', 'age': 30, 'city': 'New York'},
{'name': 'Bob', 'age': 25, 'city': 'San Francisco'},
{'name': 'Charlie', 'age': 35, 'city': 'Los Angeles'}]
<class 'list'>


Similar Reads

Python - Convert Dictionaries List to Order Key Nested dictionaries
Given list of dictionaries, convert to ordered key dictionary with each key contained dictionary as its nested value. Input : test_list = [{"Gfg" : 3, 4 : 9}, {"is": 8, "Good" : 2}] Output : {0: {'Gfg': 3, 4: 9}, 1: {'is': 8, 'Good': 2}} Explanation : List converted to dictionary with index keys. Input : test_list = [{"is": 8, "Good" : 2}] Output :
6 min read
Python - Convert list of dictionaries to Dictionary Value list
Given a list of dictionary, convert to dictionary with same key mapped with all values in as value list. Input : test_list = [{"Gfg" : 6, "is" : 9, "best" : 10}, {"Gfg" : 8, "is" : 11, "best" : 19}] Output : {'Gfg': [6, 8], 'is': [9, 11], 'best': [10, 19]} Explanation : 6, 8 of "Gfg" mapped as value list, similarly every other. Input : test_list =
10 min read
Convert a List of Dictionaries into a Set of Dictionaries
Python's versatility allows developers to manipulate data in various ways. When working with a list of dictionaries, there might be scenarios where you want to convert it into a set of dictionaries to eliminate duplicates or for other reasons. In this article, we'll explore three different methods to achieve this goal with code examples. Convert A
3 min read
Python Program to Convert dictionary string values to List of dictionaries
Given a dictionary with values as delimiter separated values, the task is to write a python program to convert each string as different value in list of dictionaries. Input : test_dict = {"Gfg" : "1:2:3", "best" : "4:8:11"} Output : [{'Gfg': '1', 'best': '4'}, {'Gfg': '2', 'best': '8'}, {'Gfg': '3', 'best': '11'}] Explanation : List after dictionar
2 min read
Python - Convert list of dictionaries to dictionary of lists
In this article, we will discuss how to convert a list of dictionaries to a dictionary of lists. Method 1: Using for loop By iterating based on the first key we can convert list of dict to dict of list. Python program to create student list of dictionaries C/C++ Code # create a list of dictionaries # with student data data = [ {'name': 'sravan', 's
3 min read
Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
Given two dictionaries. The task is to merge them in such a way that the resulting dictionary contains the key from the first dictionary and the value from the second dictionary. Examples: Input : test_dict1 = {"Gfg" : 20, "is" : 36, "best" : 100}, test_dict2 = {"Gfg2" : 26, "is2" : 20, "best2" : 70} Output : {'Gfg': 26, 'is': 20, 'best': 70} Expla
8 min read
Python Program to extract Dictionaries with given Key from a list of dictionaries
Given a list of dictionaries, the task is to write a python program that extracts only those dictionaries that contain a specific given key value. Input : test_list = [{'gfg' : 2, 'is' : 8, 'good' : 3}, {'gfg' : 1, 'for' : 10, 'geeks' : 9}, {'love' : 3}], key= "gfg"Output : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg' : 1, 'for' : 10, 'geeks' : 9}] Expl
6 min read
Convert Dictionary Value list to Dictionary List Python
Sometimes, while working with Python Dictionaries, 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 =
9 min read
Python - Convert dictionary to K sized dictionaries
Given a Dictionary, divide dictionary into K sized different dictionaries list. Input : test_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'geeks' : 5, 'CS' : 6}, K = 3 Output : [{'Gfg': 1, 'is': 2, 'best': 3}, {'for': 4, 'geeks': 5, 'CS': 6}] Explanation : Divided into size of 3 keys. Input : test_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3, 'fo
4 min read
Python - Convert Flat dictionaries to Nested dictionary
Sometimes, while working with records, we can have a problem in which we need to perform the task of conversion of multiple flat dictionaries to a single nested dictionary. This can have applications in many domains in which data is used extensively. Let's discuss certain ways by which we can convert flat dictionaries into nested dictionaries. Conv
4 min read
Practice Tags :