Python – Convert list of dictionaries to dictionary of lists

07 Jan, 2022
In this article, we will discuss how to convert a list of dictionaries to a dictionary of lists.
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 datadata = [    {'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']} ] # displaydata
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']}]
Example: Convert list of dictionary to a dictionary of list
Python3
# create an empty dictionarydictionary_of_list = {} # for loop to convert list of dict# to dict of listfor item in data:    name = item['name']    dictionary_of_list[name] = item # displaydictionary_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']}}
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 dictionarydata = [{'manoj': 'java', 'bobby': 'python'},        {'manoj': 'php', 'bobby': 'java'},        {'manoj': 'cloud', 'bobby': 'big-data'}] # convert into dictionary of list# with list as valuesprint({key: [i[key] for i in data] for key in data[0]})
Output:
{'manoj': ['java', 'php', 'cloud'], 'bobby': ['python', 'java', 'big-data']}
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 pandasimport pandas as pd # consider the list of dictionarydata = [{'manoj': 'java', 'bobby': 'python'},        {'manoj': 'php', 'bobby': 'java'},        {'manoj': 'cloud', 'bobby': 'big-data'}] # convert into dictionary of list# with list as values using pandas dataframepd.DataFrame(data).to_dict(orient="list")
Output:
{‘bobby’: [‘python’, ‘java’, ‘big-data’], ‘manoj’: [‘java’, ‘php’, ‘cloud’]}
Numpy is used to process the arrays, we can get the values by using key or index
Python3
# import numpy moduleimport numpy as np # create numpy datadata = np.array([(10, 20), (50, 60)],                dtype=[('value1', int),                       ('value2', int)]) # display by using indexprint(data[0]) # display by using keyprint(data['value1'])
Output:
(10, 20)
[10 50]
prachisoda1234
Picked
Python dictionary-programs
Python list-programs
python-dict
python-list
Python
Python Programs
python-dict
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.