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
# create a list of dictionaries # with student data 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' ]} ] # display 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
# create an empty dictionary dictionary_of_list = {} # for loop to convert list of dict # to dict of list for item in data: name = item[ 'name' ] dictionary_of_list[name] = item # display 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)
Method 2: Using dictionary comprehension
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
# consider the list of dictionary data = [{ 'manoj' : 'java' , 'bobby' : 'python' }, { 'manoj' : 'php' , 'bobby' : 'java' }, { 'manoj' : 'cloud' , 'bobby' : 'big-data' }] # convert into dictionary of list # with list as values print ({key: [i[key] for i in data] for key in data[ 0 ]}) |
{'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)
Method 3: Using pandas DataFrame
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 import pandas as pd # consider the list of dictionary data = [{ 'manoj' : 'java' , 'bobby' : 'python' }, { 'manoj' : 'php' , 'bobby' : 'java' }, { 'manoj' : 'cloud' , 'bobby' : 'big-data' }] # convert into dictionary of list # with list as values using pandas dataframe 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 module import numpy as np # create numpy data data = np.array([( 10 , 20 ), ( 50 , 60 )], dtype = [( 'value1' , int ), ( 'value2' , int )]) # display by using index print (data[ 0 ]) # display by using key print (data[ 'value1' ]) |
Output:
(10, 20) [10 50]
Time Complexity: O(n)
Auxiliary Space: O(n)