Python - Multiple Keys Grouped Summation


The given problem statement is to get the grouped summation of the same key in the given list of tuples. So we will use Python functionalities to write the program for this problem.

Understanding the Problem

The problem at hand is to calculate the sum of values in the given input list data as per the multiple keys and this process is known as the Multiple Keys Grouped Summation. So we will be given data with the key and value pairs. Our task is to group the values as per the multiple keys and we have to calculate the sum for every group in the given list.

Logic for The Above Problem

For solving this problem, we will be using a dictionary to save the cumulative addition for the current group. And we will traverse through the list data and get the key values. With the help of keys we will add the respective values to the cumulative sum for every group. And if the group for the particular value is not present in the dictionary then we will start it with the current value. In the end we will be having the sum for every group in the given list data.

Algorithm

  • Step 1 − First we will import the necessary modules. So import the defaultdict class from the collections module in Python.

  • Step 2 − Then initialize the list_data which is containing the input data.

  • Step 3 − After that create an empty defaultdict as grouped_sum with the initial value of 0.

  • Step 4 − Next, iterate each data in the list tuple. And get the key values from the tuple.

  • Step 5 − Also extract the value associated with the tuple. And add the value to the cumulative summation for the respective key in the grouped_sum.

  • Step 6 − Then we will create the new list as Output by traversing the items in the grouped_sum and for every key-value pair we will create a new tuple with the sum value.

  • Step 7 − Print the initial input list and also Output list to show the multiple keys grouped summation.

Example

# Import the defaultdict
from collections import defaultdict

# Initialize the list data
list_data = [
   (1000, 2022, 1),
   (1500, 2022, 2),
   (2000, 2022, 1),
   (500, 2023, 3),
   (800, 2023, 1),
   (1200, 2023, 1),
   (1500, 2023, 3)
]

print("The input list is: " + str(list_data),'\n')

grouped_sum = defaultdict(int)

for data in list_data:
   # Get the key values and also value
   key = data[1:3]
   value = data[0]  
   grouped_sum[key] += value

Output = [(key[0], key[1], value) for key, value in grouped_sum.items()]

# Printing the Output
print("The Multiple Keys Grouped Summation: " + str(Output))

Output

The input list is: [(1000, 2022, 1), (1500, 2022, 2), (2000, 2022, 1), (500, 2023, 3), (800, 2023, 1), (1200, 2023, 1), (1500, 2023, 3)] 

The Multiple Keys Grouped Summation: [(2022, 1, 3000), (2022, 2, 1500), (2023, 3, 2000), (2023, 1, 2000)]

Complexity

If the number of key and value pairs in the input data is n then the time complexity to compute the sum of the values having the same keys is O(n). As we have iterated over each pair once and performed a constant time operation to get the required sum or values.

Conclusion

We have effectively implemented the code to Multiple Keys Grouped Summation with the help of dictionary to save the cumulative sums for each key-value pairs. And we have used a built-in class called defaultdict from collections module in Python. The code has a time complexity of O(n).

Updated on: 17-Oct-2023

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements