Python - Convert List of Dictionaries to List of Lists
Last Updated :
21 Jan, 2025
We are given list of dictionaries we need to convert it to list of lists. For example we are given a list of dictionaries a = [{'name': 'Geeks', 'age': 25}, {'name': 'Geeks', 'age': 30}] we need to convert it in list of list so that the output becomes[['Geeks',25],['Geeks;'30]].
Using List Comprehension
List comprehension iterates over each dictionary in the list, extracting the values of the dictionary. These values are then converted into individual lists and gathered into a new list.
Python
a = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}] # List of dictionaries
# Use list comprehension to iterate over each dictionary, extracting its values and converting them into a list
res = [[value for value in d.values()] for d in a]
print(res)
Output[['Alice', 25], ['Bob', 30]]
Explanation:
- List comprehension iterates over each dictionary in a, using d.values() to extract the values of each dictionary.
- These extracted values are then collected into a list, resulting in a new list of lists where each sublist contains the values from a dictionary.
Using map()
Using map() the function can apply the operation of extracting values from each dictionary and converting them into a list.
Python
a = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}] # List of dictionaries
# Use map with a lambda function to extract the values from each dictionary and convert them into a list
res = list(map(lambda d: list(d.values()), a))
print(res)
Output[['Alice', 25], ['Bob', 30]]
Explanation:
- map() function applies a lambda to each dictionary in a, using d.values() to extract the dictionary's values.
- list() function collects the results of map() converting them into a list of lists where each sublist contains the values from a dictionary.
Using itertools.chain()
Using itertools.chain(), we can flatten a list of dictionaries into a single sequence of values.
Python
from itertools import chain # Importing chain from itertools module
a = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}] # List of dictionaries
# Use list comprehension to extract values from each dictionary, then flatten the list of lists using chain.from_iterable
res = list(chain.from_iterable([list(d.values()) for d in a]))
print(res)
Output['Alice', 25, 'Bob', 30]
Explanation:
- List comprehension is used to iterate over each dictionary in a, extracting its values and converting them into a list.
- itertools.chain.from_iterable() flattens the list of lists into a single list, combining all the extracted values into one sequence.
Using pandas.DataFrame
Using pandas.DataFrame, the list of dictionaries is converted into a DataFrame, where each dictionary represents a row. Then, df.values.tolist() is used to convert the DataFrame's values into a list of lists.
Python
import pandas as pd # Importing pandas library
a = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}] # List of dictionaries
# Convert the list of dictionaries to a DataFrame
df = pd.DataFrame(a)
# Convert the DataFrame values to a list of lists using .values.tolist()
res = df.values.tolist()
print(res)
Output[['Alice', 25], ['Bob', 30]]
Explanation:
- List of dictionaries a is converted into a pandas DataFrame, where each dictionary becomes a row.
- .values.tolist() method is used to convert the DataFrame's values into a list of lists, where each sublist represents a row's values.
Similar Reads
Convert List of Dictionaries to Dictionary of Lists - Python We are given a list of dictionaries we need to convert it to dictionaries of lists. For example, we are given a list of dictionaries li = [{'manoj': 'java', 'bobby': 'python'}, {'manoj': 'php', 'bobby': 'java'}, {'manoj': 'cloud', 'bobby': 'big-data'}] we need to convert this to dictionary of list s
3 min read
Python - Convert List to List of dictionaries We are given a lists with key and value pair we need to convert the lists to List of dictionaries. For example we are given two list a=["name", "age", "city"] and b=[["Geeks", 25, "New York"], ["Geeks", 30, "Los Angeles"], ["Geeks", 22, "Chicago"]] we need to convert these keys and values list into
4 min read
Convert List of Lists to Dictionary - Python We are given list of lists we need to convert it to python . For example we are given a list of lists a = [["a", 1], ["b", 2], ["c", 3]] we need to convert the list in dictionary so that the output becomes {'a': 1, 'b': 2, 'c': 3}. Using Dictionary ComprehensionUsing dictionary comprehension, we ite
3 min read
Python - Convert Key-Value list Dictionary to List of Lists We are given a key value list dictionary we need to convert it list of lists. For example we are given a dictionary a = {'name': 'Geeks', 'age': 8, 'city': 'Noida'} we need to convert this into list of lists so the output should be [['name', 'Geeks'], ['age', 25], ['city', 'Geeks']]. Using List Comp
2 min read
Python - Convert list of dictionaries to Dictionary Value list 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 ComprehensionUsing dictionary comprehension, we can
3 min read