Python - Convert list of dictionaries to Dictionary Value list
Last Updated :
15 Jul, 2025
We are given a list of dictionaries we need to convert it to dictionary. For example, given a list of dictionaries: d = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}], the output should be: {'a': [1, 3, 5], 'b': [2, 4, 6]}.
Using Dictionary Comprehension
Using dictionary comprehension, we can convert a list of dictionaries into a dictionary of value lists by iterating over the keys of the first dictionary to define the keys of the result.
Python
d = [
{'name': 'Alice', 'age': 25, 'city': 'New York'},
{'name': 'Bob', 'age': 30, 'city': 'Los Angeles'},
{'name': 'Charlie', 'age': 35, 'city': 'Chicago'}
]
res = {key: [d[key] for d in d] for key in d[0].keys()}
print(res)
Output{'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'city': ['New York', 'Los Angeles', 'Chicago']}
Explanation:
- Outer comprehension iterates over the keys of the first dictionary to define the result's keys.
- Inner comprehension collects values for each key from all dictionaries in the list.
Using defaultdict from collections
Using defaultdict from collections, we can convert a list of dictionaries into a dictionary of value lists by appending values to lists for each key dynamically. This approach handles keys efficiently without requiring prior initialization.
Python
from collections import defaultdict
d = [
{'name': 'Alice', 'age': 25, 'city': 'New York'},
{'name': 'Bob', 'age': 30, 'city': 'Los Angeles'},
{'name': 'Charlie', 'age': 35, 'city': 'Chicago'}
]
res = defaultdict(list)
for d in d:
for key, value in d.items():
res[key].append(value)
print(dict(res))
Output{'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'city': ['New York', 'Los Angeles', 'Chicago']}
Explanation:
- Res dictionary is initialized as a defaultdict with list, allowing each key to automatically have an empty list as its default value.
- For each dictionary in the list, the inner loop iterates through its key-value pairs, appending each value to the corresponding key's list in res
Using pandas
Using pandas, we can convert a list of dictionaries into a DataFrame and then use the values.tolist() method to extract the values as a list of lists. This approach efficiently handles structured data and converts it into the desired format.
Python
import pandas as pd
# Define a list of dictionaries
d = [
{'name': 'Alice', 'age': 25, 'city': 'New York'},
{'name': 'Bob', 'age': 30, 'city': 'Los Angeles'},
{'name': 'Charlie', 'age': 35, 'city': 'Chicago'}
]
# Convert the list of dictionaries into a pandas DataFrame
df = pd.DataFrame(d)
# Convert the DataFrame into a dictionary with lists of values for each column using 'orient="list"'
res = df.to_dict(orient='list')
# Print the resulting dictionary
print(res)
Output{'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'city': ['New York', 'Los Angeles', 'Chicago']}
Explanation:
- List of dictionaries is converted into a pandas DataFrame, where each dictionary becomes a row and each key becomes a column header.
- DataFrame is then converted to a dictionary using to_dict(orient='list'), where each column name maps to a list of its corresponding values.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice