Horje
Convert Dictionary of Dictionaries to Python List of Dictionaries

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'>



Reffered: https://www.geeksforgeeks.org


Python

Related
Filtering a List of Dictionary on Multiple Values in Python Filtering a List of Dictionary on Multiple Values in Python
Create Dynamic Dictionary in Python Create Dynamic Dictionary in Python
How To Update Keras? How To Update Keras?
Install Psycopg2 using PIP in Python Install Psycopg2 using PIP in Python
Lists Of Strings In Python Lists Of Strings In Python

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14