VOOZH about

URL: https://www.geeksforgeeks.org/python/python-convert-list-of-dictionaries-to-dictionary-value-list/

⇱ Python - Convert list of dictionaries to Dictionary Value list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


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 defaultdictfrom 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.


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.


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.
Comment