In this article, we will discuss how to convert a list of dictionaries to a dictionary of lists.
Method 1: Using for loop
By iterating based on the first key we can convert list of dict to dict of list. Python program to create student list of dictionaries
Python3
data = [
{'name': 'sravan', 'subjects': ['java', 'python']},
{'name': 'bobby', 'subjects': ['c/cpp', 'java']},
{'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
{'name': 'rohith', 'subjects': ['php', 'os']},
{'name': 'gnanesh', 'subjects': ['html', 'sql']}
]
data
Output:
[{'name': 'sravan', 'subjects': ['java', 'python']},
{'name': 'bobby', 'subjects': ['c/cpp', 'java']},
{'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
{'name': 'rohith', 'subjects': ['php', 'os']},
{'name': 'gnanesh', 'subjects': ['html', 'sql']}]
Time Complexity: O(n)
Auxiliary Space: O(n)
Example: Convert list of dictionary to a dictionary of list
Python3
dictionary_of_list = {}
for item in data:
name = item['name']
dictionary_of_list[name] = item
dictionary_of_list
Output:
{'bobby': {'name': 'bobby', 'subjects': ['c/cpp', 'java']},
'gnanesh': {'name': 'gnanesh', 'subjects': ['html', 'sql']},
'ojsawi': {'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
'rohith': {'name': 'rohith', 'subjects': ['php', 'os']},
'sravan': {'name': 'sravan', 'subjects': ['java', 'python']}}
Time Complexity: O(n), where n is the length of the given dictionary Auxiliary Space: O(n)
Here we are using a dictionary comprehension to convert a list of dictionaries into the dictionary of the list, so we are passing the list as values based on keys in the new dictionary
Syntax : {key: [i[key] for i in list_of_dictionary] for key in list_of_dictionary[0]}
where
list_of_dictionary is the input
key is the key in list_of_dictionary
Example: Program to convert student list of dictionary into dictionary of list
Python3
data = [{'manoj': 'java', 'bobby': 'python'},
{'manoj': 'php', 'bobby': 'java'},
{'manoj': 'cloud', 'bobby': 'big-data'}]
print({key: [i[key] for i in data] for key in data[0]})
Output
{'manoj': ['java', 'php', 'cloud'], 'bobby': ['python', 'java', 'big-data']}
Time Complexity: O(n), where n is the length of the given dictionary Auxiliary Space: O(n)
By using pandas dataframe we will convert list of dict to dict of list
Syntax : pandas.DataFrame(list_of_dictionary).to_dict(orient=”list”)
Where:
list_of_dictionary is the input
to_dict() method is to convert into dictionary
orient parameter is to convert into list
Example :
Python3
import pandas as pd
data = [{'manoj': 'java', 'bobby': 'python'},
{'manoj': 'php', 'bobby': 'java'},
{'manoj': 'cloud', 'bobby': 'big-data'}]
pd.DataFrame(data).to_dict(orient="list")
Output :
{‘bobby’: [‘python’, ‘java’, ‘big-data’], ‘manoj’: [‘java’, ‘php’, ‘cloud’]}
Time Complexity: O(n), where n is the length of the given list of dictionary Auxiliary Space: O(n)
Method 4: Using NumPy
Numpy is used to process the arrays, we can get the values by using key or index
Python3
import numpy as np
data = np.array([(10, 20), (50, 60)],
dtype=[('value1', int),
('value2', int)])
print(data[0])
print(data['value1'])
Output:
(10, 20)
[10 50]
Time Complexity: O(n) Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Feb, 2023
Like Article
Save Article