Python - Convert List of Dictionaries to List of Lists
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.
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.
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.
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.
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.