Python - Summation Grouping in Dictionary List
Last Updated :
05 Apr, 2023
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to perform the grouping of dictionaries according to specific key, and perform summation of certain keys while grouping similar key's value. This is s peculiar problem but can have applications in domains such as web development. Let's discuss a certain way in which this task can be performed.
Input : test_list = [{'geeks': 10, 'best': 8, 'Gfg': 6}, {'geeks': 12, 'best': 11, 'Gfg': 4}]
Output : {4: {'geeks': 12, 'best': 11}, 6: {'geeks': 10, 'best': 8}}
Input : test_list = [{'CS': 14, 'best': 9, 'geeks': 7, 'Gfg': 14}]
Output : {14: {'best': 9, 'geeks': 7}}
Method #1 : Using loop This is brute force way in which this task can be performed. In this we manually loop through each key from dictionary and perform required grouping according to the key and construct the required grouped and summed dictionary.
Python3
# Python3 code to demonstrate working of
# Summation Grouping in Dictionary List
# Using loop
# initializing list
test_list = [{'Gfg' : 1, 'id' : 2, 'best' : 8, 'geeks' : 10},
{'Gfg' : 4, 'id' : 4, 'best': 10, 'geeks' : 12},
{'Gfg' : 4, 'id' : 8, 'best': 11, 'geeks' : 15}]
# printing original list
print("The original list is : " + str(test_list))
# initializing group key
grp_key = 'Gfg'
# initializing sum keys
sum_keys = ['best', 'geeks']
# Summation Grouping in Dictionary List
# Using loop
res = {}
for sub in test_list:
ele = sub[grp_key]
if ele not in res:
res[ele] = {x: 0 for x in sum_keys}
for y in sum_keys:
res[ele][y] += int(sub[y])
# printing result
print("The grouped list : " + str(res))
Output :
The original list is : [{'geeks': 10, 'id': 2, 'best': 8, 'Gfg': 1}, {'geeks': 12, 'id': 4, 'best': 10, 'Gfg': 4}, {'geeks': 15, 'id': 8, 'best': 11, 'Gfg': 4}] The grouped list : {1: {'geeks': 10, 'best': 8}, 4: {'geeks': 27, 'best': 21}}
Time complexity: O(nm), where n is the length of the input list and m is the number of sum keys.
Auxiliary space: O(nm), because the program creates a dictionary with n keys, and each key has an associated dictionary of size m.
Method #2: Using defaultdict
Use defaultdict with a lambda function that returns a dictionary with the keys from the sum_keys list and the initial values set to 0 is used. When a new group key is found, the default value is created and the sum of the values for the corresponding keys is calculated. Finally, the res dictionary is converted to a regular dictionary using the dict() function before being printed.
Python3
from collections import defaultdict
test_list = [{'Gfg' : 1, 'id' : 2, 'best' : 8, 'geeks' : 10},
{'Gfg' : 4, 'id' : 4, 'best': 10, 'geeks' : 12},
{'Gfg' : 4, 'id' : 8, 'best': 11, 'geeks' : 15}]
grp_key = 'Gfg'
sum_keys = ['best', 'geeks']
res = defaultdict(lambda: {x: 0 for x in sum_keys})
for sub in test_list:
ele = sub[grp_key]
for y in sum_keys:
res[ele][y] += int(sub[y])
print("The grouped list: ", dict(res))
OutputThe grouped list: {1: {'best': 8, 'geeks': 10}, 4: {'best': 21, 'geeks': 27}}
Time complexity: O(N*M), where N is the number of dictionaries in the test_list and M is the number of keys in each dictionary that need to be summed (in this case, 2: 'best' and 'geeks').
Auxiliary space: O(K*N), where K is the number of keys in each dictionary and N is the number of dictionaries in the test_list.
Method 3 : Using list comprehension and the built-in function sum()
- Define the input data and the grouping and summarizing keys:
- Use list comprehension to create a new list of dictionaries where each dictionary contains the group key and the sums of the values for the sum keys:
- Print the result list:
Python3
test_list = [{'Gfg': 1, 'id': 2, 'best': 8, 'geeks': 10},
{'Gfg': 4, 'id': 4, 'best': 10, 'geeks': 12},
{'Gfg': 4, 'id': 8, 'best': 11, 'geeks': 15}]
grp_key = 'Gfg'
sum_keys = ['best', 'geeks']
result_list = [{grp_key: key, **{skey: sum(sub[skey] for sub in test_list if sub[grp_key] == key)}}
for key in set(sub[grp_key] for sub in test_list)
for skey in sum_keys]
print("The grouped list: ", result_list)
OutputThe grouped list: [{'Gfg': 1, 'best': 8}, {'Gfg': 1, 'geeks': 10}, {'Gfg': 4, 'best': 21}, {'Gfg': 4, 'geeks': 27}]
The time complexity of this method is O(n*m), where n is the number of dictionaries in the input list and m is the number of keys in the sum_keys list.
The auxiliary space is O(n), where n is the number of groups
Method 4: Using the pandas library
Step-by-step approach:
- Import the pandas library.
- Create a DataFrame from the given list test_list.
- Group the DataFrame by the group key grp_key.
- Sum the values of the sum keys sum_keys for each group.
- Convert the resulting DataFrame back to a dictionary.
Below is the implementation of the above approach:
Python3
# importing pandas library
import pandas as pd
# initializing list
test_list = [{'Gfg' : 1, 'id' : 2, 'best' : 8, 'geeks' : 10},
{'Gfg' : 4, 'id' : 4, 'best': 10, 'geeks' : 12},
{'Gfg' : 4, 'id' : 8, 'best': 11, 'geeks' : 15}]
# creating a DataFrame from the list
df = pd.DataFrame(test_list)
# initializing group key
grp_key = 'Gfg'
# initializing sum keys
sum_keys = ['best', 'geeks']
# grouping the DataFrame by the group key and summing the sum keys
res_df = df.groupby(grp_key)[sum_keys].sum()
# converting the resulting DataFrame back to a dictionary
res = res_df.to_dict('index')
# printing result
print("The grouped list: " + str(res))
OUTPUT:
The grouped list: {1: {'best': 8, 'geeks': 10}, 4: {'best': 21, 'geeks': 27}}
Time complexity: O(n), where n is the number of dictionaries in the given list test_list.
Auxiliary space: O(n), as we are creating a DataFrame and a dict with the same number of entries as test_list.
Similar Reads
Python | Grouping list values into dictionary
Sometimes, while working with data, we can be encountered a situation in which we have a list of lists and we need to group its 2nd index with the common initial element in lists. Let's discuss ways in which this problem can be solved. Method 1: Using defaultdict() + loop + dict() The defaultdict ca
7 min read
Python - Group Similar keys in dictionary
Sometimes while working with dictionary data, we can have problems in which we need to perform grouping based on the substring of keys and reform the data grouped on similar keys. This can have application in data preprocessing. Let us discuss certain ways in which this task can be performed. Method
5 min read
Python - Group Similar items to Dictionary Values List
We are given a list of items and our task is to group similar elements together as dictionary values. The keys will be the unique items and their values will be lists containing all occurrences of that item. For example, given ['apple', 'banana', 'apple', 'orange', 'banana'], the output should be: {
2 min read
Python | i^k Summation in list
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding sum of i^k of list in just one line. Letâs discuss cer
5 min read
Python | Summation of dictionary list values
Sometimes, while working with Python dictionaries, we can have its values as lists. In this can, we can have a problem in that we just require the count of elements in those lists as a whole. This can be a problem in Data Science in which we need to get total records in observations. Let's discuss c
6 min read
Python | Value summation of key in dictionary
Many operations such as grouping and conversions are possible using Python dictionaries. But sometimes, we can also have a problem in which we need to perform the aggregation of values of key in dictionary list. This task is common in day-day programming. Let's discuss certain ways in which this tas
6 min read
Python - Nested Dictionary values summation
Sometimes, while working with Python dictionaries, we can have problem in which we have nested records and we need cumulative summation of it's keys values. This can have possible application in domains such as web development and competitive programming. Lets discuss certain ways in which this task
8 min read
Python - Group list of tuples to dictionary
The task is to convert a list of tuples into a dictionary, where each tuple consists of two elements. The first element of each tuple becomes the key and the second element becomes the value. If a key appears multiple times, its corresponding values should be grouped together, typically in a list.Fo
3 min read
Python - Summation of Custom nested keys in Dictionary
Given a dictionary with keys as nested dictionaries, find the sum of values of certain custom keys inside the nested dictionary. Input : test_dict = {'Gfg' : {1 : 6, 5: 9, 9: 12}, 'is' : {1 : 9, 5: 7, 9: 2}, 'best' : {1 : 3, 5: 4, 9: 14}}, sum_key = [1] Output : 18 Explanation : 6 + 9 + 3 = 18, only
10 min read
Python | Grouping dictionary keys by value
While performing computations over dictionary, we can come across a problem in which we might have to perform the task of grouping keys according to value, i.e create a list of keys, it is value of. This can other in cases of organising data in case of machine learning. Let's discuss certain way in
4 min read