GeeksforGeeks App
Open App
Browser
Continue

# Python – Convert list of dictionaries to Dictionary Value list

Given a list of dictionary, convert to dictionary with same key mapped with all values in as value list.

Input : test_list = [{“Gfg” : 6, “is” : 9, “best” : 10}, {“Gfg” : 8, “is” : 11, “best” : 19}]
Output : {‘Gfg’: [6, 8], ‘is’: [9, 11], ‘best’: [10, 19]}
Explanation : 6, 8 of “Gfg” mapped as value list, similarly every other.

Input : test_list = [{“Gfg” : 6, “best” : 10}, {“Gfg” : 8, “best” : 19}]
Output : {‘Gfg’: [6, 8], ‘best’: [10, 19]}
Explanation : Same as above, conversion.

Method #1 : Using loop

This is brute way to solve this problem. In this, we iterate through all the dictionaries, and extract each key and convert to required dictionary in nested loops.

## Python3

 `# Python3 code to demonstrate working of``# Convert list of dictionaries to Dictionary Value list``# Using loop``from` `collections ``import` `defaultdict` `# initializing lists``test_list ``=` `[{``"Gfg"` `: ``6``, ``"is"` `: ``9``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``8``, ``"is"` `: ``11``, ``"best"` `: ``19``},``             ``{``"Gfg"` `: ``2``, ``"is"` `: ``16``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``12``, ``"is"` `: ``1``, ``"best"` `: ``8``},``             ``{``"Gfg"` `: ``22``, ``"is"` `: ``6``, ``"best"` `: ``8``}]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# using loop to get dictionaries``# defaultdict used to make default empty list``# for each key``res ``=` `defaultdict(``list``)``for` `sub ``in` `test_list:``    ``for` `key ``in` `sub:``        ``res[key].append(sub[key])``    ` `# printing result``print``(``"The extracted dictionary : "` `+` `str``(``dict``(res)))`

Output

```The original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The extracted dictionary : {'Gfg': [6, 8, 2, 12, 22], 'is': [9, 11, 16, 1, 6], 'best': [10, 19, 10, 8, 8]}```

Time Complexity: O(n*n), where n is the elements of dictionary
Auxiliary Space: O(n), where n is the size of dictionary

Method #2 : Using list comprehension + dictionary comprehension

The combination of above functionalities can be used to solve this problem. In this, we use dictionary comprehension to construct dictionary and list comprehension is used to extract values from original list.

## Python3

 `# Python3 code to demonstrate working of``# Convert list of dictionaries to Dictionary Value list``# Using list comprehension + dictionary comprehension``from` `collections ``import` `defaultdict` `# initializing lists``test_list ``=` `[{``"Gfg"` `: ``6``, ``"is"` `: ``9``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``8``, ``"is"` `: ``11``, ``"best"` `: ``19``},``             ``{``"Gfg"` `: ``2``, ``"is"` `: ``16``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``12``, ``"is"` `: ``1``, ``"best"` `: ``8``},``             ``{``"Gfg"` `: ``22``, ``"is"` `: ``6``, ``"best"` `: ``8``}]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# dictionary and list comprehension``# for shorthand to solution of problem``res ``=` `defaultdict(``list``)``{res[key].append(sub[key]) ``for` `sub ``in` `test_list ``for` `key ``in` `sub}``    ` `# printing result``print``(``"The extracted dictionary : "` `+` `str``(``dict``(res)))`

Output

```The original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The extracted dictionary : {'Gfg': [6, 8, 2, 12, 22], 'is': [9, 11, 16, 1, 6], 'best': [10, 19, 10, 8, 8]}```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3: Using  the zip() and map() functions:

This code first extracts all the keys from the dictionaries in the list using the set() and union() methods. Then, for each key, it maps the values from the dictionaries in the list to a list using the map() and lambda functions, and appends the resulting list to a list of values. Finally, it creates a dictionary with the key-value pairs using the zip() function, and prints the result.

## Python3

 `# Python3 code to demonstrate working of``# Convert list of dictionaries to Dictionary Value list``# Using zip() and map() functions` `# initializing lists``test_list ``=` `[{``"Gfg"` `: ``6``, ``"is"` `: ``9``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``8``, ``"is"` `: ``11``, ``"best"` `: ``19``},``             ``{``"Gfg"` `: ``2``, ``"is"` `: ``16``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``12``, ``"is"` `: ``1``, ``"best"` `: ``8``},``             ``{``"Gfg"` `: ``22``, ``"is"` `: ``6``, ``"best"` `: ``8``}]` `# get all keys from the dictionaries``keys ``=` `set``().union(``*``test_list)` `# extract values for each key``values ``=` `[``list``(``map``(``lambda` `x: x[key], test_list)) ``for` `key ``in` `keys]` `# create dictionary with key-value pairs``result_dict ``=` `dict``(``zip``(keys, values))` `# print the result``print``(``"The extracted dictionary : "` `+` `str``(result_dict))`

Output

`The extracted dictionary : {'best': [10, 19, 10, 8, 8], 'Gfg': [6, 8, 2, 12, 22], 'is': [9, 11, 16, 1, 6]}`

Time Complexity: O(n*k), where n is the number of dictionaries in the list and k is the maximum number of keys in any dictionary.
Auxiliary Space: O(nk), where n is the number of dictionaries in the list and k is the maximum number of keys in any dictionary.

Method 4: Using defaultdict from the collections module to create the dictionary with default values as empty lists.

1. Import the defaultdict class from the collections module.
2. Initialize a defaultdict object with the default factory as list.
3. Iterate through each dictionary in the list using a for loop.
4. For each dictionary, iterate through its key-value pairs using another for loop.
5. Use the key to access the corresponding list in the defaultdict object and append the value to it.
6. Return the resulting defaultdict object as a regular dictionary.

## Python3

 `from` `collections ``import` `defaultdict` `# initializing lists``test_list ``=` `[{``"Gfg"` `: ``6``, ``"is"` `: ``9``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``8``, ``"is"` `: ``11``, ``"best"` `: ``19``},``             ``{``"Gfg"` `: ``2``, ``"is"` `: ``16``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``12``, ``"is"` `: ``1``, ``"best"` `: ``8``},``             ``{``"Gfg"` `: ``22``, ``"is"` `: ``6``, ``"best"` `: ``8``}]` `# create defaultdict object with default factory as list``result_dict ``=` `defaultdict(``list``)` `# iterate through each dictionary and append key-value pairs to result_dict``for` `d ``in` `test_list:``    ``for` `key, value ``in` `d.items():``        ``result_dict[key].append(value)` `# convert defaultdict object to regular dictionary``result_dict ``=` `dict``(result_dict)` `# print the result``print``(``"The extracted dictionary : "` `+` `str``(result_dict))`

Output

`The extracted dictionary : {'Gfg': [6, 8, 2, 12, 22], 'is': [9, 11, 16, 1, 6], 'best': [10, 19, 10, 8, 8]}`

Time complexity: O(nm), where n is the number of dictionaries in the list and m is the average number of key-value pairs in each dictionary.
The auxiliary space complexity is also O(nm), since we need to store all the key-value pairs in the resulting dictionary.

Method #5: Using pandas library

1. Import the pandas library.
2. Convert the list of dictionaries to a pandas DataFrame using the pd.DataFrame() method.
3. Use the to_dict() method with the orient=’list’ parameter to convert the DataFrame to a dictionary with values as lists.
4. Print the resulting dictionary.

## Python3

 `import` `pandas as pd` `# initializing lists``test_list ``=` `[{``"Gfg"` `: ``6``, ``"is"` `: ``9``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``8``, ``"is"` `: ``11``, ``"best"` `: ``19``},``             ``{``"Gfg"` `: ``2``, ``"is"` `: ``16``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``12``, ``"is"` `: ``1``, ``"best"` `: ``8``},``             ``{``"Gfg"` `: ``22``, ``"is"` `: ``6``, ``"best"` `: ``8``}]` `# convert list of dictionaries to pandas DataFrame``df ``=` `pd.DataFrame(test_list)` `# convert DataFrame to dictionary with values as lists``result_dict ``=` `df.to_dict(orient``=``'list'``)` `# print the resulting dictionary``print``(``"The extracted dictionary : "` `+` `str``(result_dict))`

Output:

`The extracted dictionary : {'Gfg': [6, 8, 2, 12, 22], 'is': [9, 11, 16, 1, 6], 'best': [10, 19, 10, 8, 8]}`

Time complexity: O(n*k), where n is the number of dictionaries in the list and k is the average number of keys per dictionary.
Auxiliary space: O(n*k), for storing the DataFrame and the resulting dictionary.

Method #6: Using reduce() function from the functools module.

Step-by-step approach:

• Import the reduce() function from the functools module.
• Use reduce() to iterate over the list of dictionaries and merge them into a single dictionary.
• Use dictionary comprehension to create a new dictionary where the keys are the original keys from the dictionaries and the values are the list of values obtained from the merged dictionary.
• Return the resulting dictionary.

Below is the implementation of the above approach:

## Python3

 `# Importing the reduce() function from the functools module``from` `functools ``import` `reduce` `# initializing lists``test_list ``=` `[{``"Gfg"` `: ``6``, ``"is"` `: ``9``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``8``, ``"is"` `: ``11``, ``"best"` `: ``19``},``             ``{``"Gfg"` `: ``2``, ``"is"` `: ``16``, ``"best"` `: ``10``},``             ``{``"Gfg"` `: ``12``, ``"is"` `: ``1``, ``"best"` `: ``8``},``             ``{``"Gfg"` `: ``22``, ``"is"` `: ``6``, ``"best"` `: ``8``}]` `# Merging the list of dictionaries into a single dictionary``merged_dict ``=` `reduce``(``lambda` `x, y: {``*``*``x, ``*``*``y}, test_list)` `# Creating a new dictionary with values as lists``result_dict ``=` `{k: [merged_dict[k] ``for` `merged_dict ``in` `test_list] ``for` `k ``in` `merged_dict}` `# Printing the resulting dictionary``print``(``"The extracted dictionary : "` `+` `str``(result_dict))`

Output

`The extracted dictionary : {'Gfg': [6, 8, 2, 12, 22], 'is': [9, 11, 16, 1, 6], 'best': [10, 19, 10, 8, 8]}`

Time complexity: O(n^2) (due to merging of dictionaries using reduce())
Auxiliary space: O(nk) (where n is the number of dictionaries in the list and k is the maximum number of keys in any of the dictionaries)

Method #7: Using heapq:

1. Initialize the list of dictionaries test_list.
2. Merge the dictionaries in the list into a single dictionary using the reduce() function from the functools module.
3. The time complexity of this step is O(n), where n is the number of dictionaries in test_list.
4. Create a new dictionary result_dict with the keys from the merged dictionary and the values as lists
5. containing the corresponding values from the original dictionaries. The time complexity of this step is O(nm),
6. where n is the number of keys in the merged dictionary and m is the number of dictionaries in test_list.
7. Print the resulting dictionary result_dict.

## Python3

 `import` `heapq` `# Initializing lists``test_list ``=` `[{``"Gfg"` `: ``6``, ``"is"` `: ``9``, ``"best"` `: ``10``},``            ``{``"Gfg"` `: ``8``, ``"is"` `: ``11``, ``"best"` `: ``19``},``            ``{``"Gfg"` `: ``2``, ``"is"` `: ``16``, ``"best"` `: ``10``},``            ``{``"Gfg"` `: ``12``, ``"is"` `: ``1``, ``"best"` `: ``8``},``            ``{``"Gfg"` `: ``22``, ``"is"` `: ``6``, ``"best"` `: ``8``}]` `# Merging the list of dictionaries into a single dictionary using heapq``merged_dict ``=` `dict``(heapq.merge(``*``[d.items() ``for` `d ``in` `test_list]))` `# Creating a new dictionary with values as lists``result_dict ``=` `{k: [d[k] ``for` `d ``in` `test_list] ``for` `k ``in` `merged_dict}` `# Printing the resulting dictionary``print``(``"The extracted dictionary : "` `+` `str``(result_dict))``#This code is contributed by Vinay pinjala.`

Output

`The extracted dictionary : {'Gfg': [6, 8, 2, 12, 22], 'is': [9, 11, 16, 1, 6], 'best': [10, 19, 10, 8, 8]}`

Time Complexity:

Creating a single dictionary from the list of dictionaries using heapq takes O(nlogk), where n is the total number of key-value pairs in all dictionaries and k is the number of dictionaries.
Creating the resulting dictionary with values as lists takes O(n), where n is the total number of key-value pairs in all dictionaries.
Therefore, the overall time complexity is O(nlogk + n).

Auxiliary Space:

The space complexity depends on the size of the resulting dictionary, which in the worst case can be the same as the total number of key-value pairs in all dictionaries, i.e., O(n).

My Personal Notes arrow_drop_up