Python | Grouped summation of tuple list
Last Updated :
27 Apr, 2023
Many times, we are given a list of tuples and we need to group its keys and perform certain operations while grouping. The most common operation is addition. Let’s discuss certain ways in which this task can be performed. Apart from addition, other operations can also be performed by doing small changes.
Method 1: Using Counter() + “+” operator
This task can be performed using the Counter function as it internally groups and an addition operator can be used to specify the functionality of the grouped result.
Python3
from collections import Counter
test_list1 = [( 'key1' , 4 ), ( 'key3' , 6 ), ( 'key2' , 8 )]
test_list2 = [( 'key2' , 1 ), ( 'key1' , 4 ), ( 'key3' , 2 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
cumul_1 = Counter( dict (test_list1))
cumul_2 = Counter( dict (test_list2))
cumul_3 = cumul_1 + cumul_2
res = list (cumul_3.items())
print ( "The grouped summation tuple list is : " + str (res))
|
Output :
The original list 1 : [('key1', 4), ('key3', 6), ('key2', 8)]
The original list 2 : [('key2', 1), ('key1', 4), ('key3', 2)]
The grouped summation tuple list is : [('key2', 9), ('key1', 8), ('key3', 8)]
Time complexity: O(n), where n is the length of the input lists.
Auxiliary Space: O(m), where m is the number of unique keys in the input lists.
Method 2: Using for loop+ “+” operator
This approach uses a dictionary to store the keys and values of the tuples. It iterates over the tuples in the list, adding the values to the corresponding keys in the dictionary. Finally, it converts the dictionary to a list of tuples.
Python3
results = {}
test_list1 = [( 'key1' , 4 ), ( 'key3' , 6 ), ( 'key2' , 8 )]
test_list2 = [( 'key2' , 1 ), ( 'key1' , 4 ), ( 'key3' , 2 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
for key, value in test_list1 + test_list2:
if key in results:
results[key] + = value
else :
results[key] = value
res = list (results.items())
print ( "The grouped summation tuple list is : " + str (res))
|
Output
The original list 1 : [('key1', 4), ('key3', 6), ('key2', 8)]
The original list 2 : [('key2', 1), ('key1', 4), ('key3', 2)]
The grouped summation tuple list is : [('key1', 8), ('key3', 8), ('key2', 9)]
Time complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using defaultdict and a for loop
- First, import the defaultdict from collections module.
- Initialize a defaultdict object named dict_sum with a default value of 0.
- Use a for loop to iterate over the concatenated list of test_list1 and test_list2. For each tuple (k, v), we add the value v to the corresponding key k in the defaultdict object dict_sum.
- Convert the resulting dictionary dict_sum to a list of tuples using the items() method, and assign it to the variable res.
Python3
from collections import defaultdict
test_list1 = [( 'key1' , 4 ), ( 'key3' , 6 ), ( 'key2' , 8 )]
test_list2 = [( 'key2' , 1 ), ( 'key1' , 4 ), ( 'key3' , 2 )]
dict_sum = defaultdict( int )
for k, v in test_list1 + test_list2:
dict_sum[k] + = v
res = list (dict_sum.items())
print ( "The grouped summation tuple list is : " + str (res))
|
Output
The grouped summation tuple list is : [('key1', 8), ('key3', 8), ('key2', 9)]
Time complexity: O(n), where n is the total number of tuples in both lists. The for loop iterates over each tuple once, and the time to update the defaultdict with a new key-value pair is O(1).
Auxiliary space: O(n). We use a defaultdict object named dict_sum to store the intermediate results.
Method 4: Using itertools.groupby() and a list comprehension
This code first merges the two input lists into a single list, and then sorts the list by key using a lambda function. The groupby function from itertools is then used to group the tuples by key, and a list comprehension is used to calculate the sum of values for each group. Finally, the result is printed.
Python3
from itertools import groupby
test_list1 = [( 'key1' , 4 ), ( 'key3' , 6 ), ( 'key2' , 8 )]
test_list2 = [( 'key2' , 1 ), ( 'key1' , 4 ), ( 'key3' , 2 )]
merged_list = sorted (test_list1 + test_list2, key = lambda x: x[ 0 ])
grouped = [(key, sum (value for _, value in group))
for key, group in groupby(merged_list, key = lambda x: x[ 0 ])]
print ( "The grouped summation tuple list is : " + str (grouped))
|
Output
The grouped summation tuple list is : [('key1', 8), ('key2', 9), ('key3', 8)]
Time complexity: O(N log N), where N is the total number of tuples in the input lists.
Auxiliary space: O(N)
Method 5: Using a dictionary comprehension.
Algorithm:
- Combine the two lists of tuples, test_list1 and test_list2.
- Create a set of unique keys present in the combined list of tuples.
- For each unique key, use a dictionary comprehension to sum up the values of all tuples in the combined list with the same key.
- Convert the resulting dictionary into a list of tuples.
- Print the resulting list of tuples.
Python3
test_list1 = [( 'key1' , 4 ), ( 'key3' , 6 ), ( 'key2' , 8 )]
test_list2 = [( 'key2' , 1 ), ( 'key1' , 4 ), ( 'key3' , 2 )]
results = {key: sum ([tup[ 1 ] for tup in test_list1 + test_list2 if tup[ 0 ] = = key])
for key in set ([tup[ 0 ] for tup in test_list1 + test_list2])}
res = list (results.items())
print ( "The grouped summation tuple list is : " + str (res))
|
Output
The grouped summation tuple list is : [('key2', 9), ('key1', 8), ('key3', 8)]
Time Complexity: O(n), where n is the total number of tuples in both lists. This is because the list comprehension and dictionary comprehension both iterate through the combined list of tuples, which takes linear time.
Auxiliary Space: O(n), where n is the total number of tuples in both lists. This is because the resulting dictionary will have at most one key-value pair for each unique key in the combined list of tuples, which can take up to n space. Additionally, the resulting list of tuples will also take up to n space.
Method 6 : using pandas library.
step by step approach:
- Import the pandas library.
- Convert both lists to pandas dataframes, with the first element of each tuple as the index and the second element as the value.
- Concatenate the dataframes using the “+” operator, grouping by the index and summing the values.
- Convert the resulting dataframe to a list of tuples using the “to_records()” method.
Python3
import pandas as pd
test_list1 = [( 'key1' , 4 ), ( 'key3' , 6 ), ( 'key2' , 8 )]
test_list2 = [( 'key2' , 1 ), ( 'key1' , 4 ), ( 'key3' , 2 )]
df1 = pd.DataFrame(test_list1, columns = [ "key" , "value" ]).set_index( "key" )
df2 = pd.DataFrame(test_list2, columns = [ "key" , "value" ]).set_index( "key" )
df3 = pd.concat([df1, df2]).groupby(level = 0 ). sum ()
res = list (df3.to_records())
print ( "The grouped summation tuple list is : " + str (res))
|
OUTPUT:
The grouped summation tuple list is : [('key1', 8), ('key2', 9), ('key3', 8)]
Time complexity: O(n log n), where n is the total number of elements in both lists. This is because the concatenation operation and the grouping operation both require sorting, which has a time complexity of O(n log n).
Auxiliary space: O(n), where n is the total number of elements in both lists. This is because the dataframes and the resulting dictionary both require memory proportional to the number of elements in the input lists.
Method 7: Using heapq:
Algorithm:
- Initialize two test lists, test_list1 and test_list2, each containing tuples of key-value pairs.
- Merge the two lists into a single list, merged_list, using the concatenation operator (+).
- Sort the merged list in ascending order by the key of each tuple, using the sorted() function and a lambda function as the key argument.
- Group the tuples in the sorted list by their key using the groupby() function from the itertools module and a lambda function as the key argument.
- For each group of tuples, calculate the sum of their values and append a new tuple to the grouped list containing the key and the sum.
- Print the final result.
Python3
import heapq
import itertools
test_list1 = [( 'key1' , 4 ), ( 'key3' , 6 ), ( 'key2' , 8 )]
test_list2 = [( 'key2' , 1 ), ( 'key1' , 4 ), ( 'key3' , 2 )]
merged_list = sorted (test_list1 + test_list2, key = lambda x: x[ 0 ])
grouped = []
for key, group in itertools.groupby(merged_list, key = lambda x: x[ 0 ]):
grouped.append((key, sum (value for _, value in group)))
print ( "The grouped summation tuple list is : " + str (grouped))
|
Output
The grouped summation tuple list is : [('key1', 8), ('key2', 9), ('key3', 8)]
Time complexity:
The time complexity of the algorithm is O(n log n), where n is the total number of tuples in the input lists. This is due to the initial sorting of the merged list and the iteration over each group of tuples.
Space complexity:
The space complexity of the algorithm is O(n), where n is the total number of tuples in the input lists. This is due to the storage of the merged list and the grouped list in memory. The groupby() function does not create additional copies of the input data, so it does not contribute to the space complexity.
Method 8: Using reduce():
Algorithm:
- Concatenate the two input lists into a single list.
- Sort the list using the key as the first element of each tuple.
- Apply reduce function to the sorted list.
- In reduce function, check if the current key is equal to the last key in the output list (res). If so, add the values together and replace the last tuple with the updated value. If not, add the current tuple to the output list.
- Return the final result.
Python3
from functools import reduce
test_list1 = [( 'key1' , 4 ), ( 'key3' , 6 ), ( 'key2' , 8 )]
test_list2 = [( 'key2' , 1 ), ( 'key1' , 4 ), ( 'key3' , 2 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
merged_list = sorted (test_list1 + test_list2, key = lambda x: x[ 0 ])
grouped = reduce ( lambda res, pair: (res[: - 1 ] + [(pair[ 0 ], res[ - 1 ][ 1 ] + pair[ 1 ])] if res and res[ - 1 ][ 0 ] = = pair[ 0 ] else res + [pair]), merged_list, [])
print ( "The grouped summation tuple list is : " + str (grouped))
|
Output
The original list 1 : [('key1', 4), ('key3', 6), ('key2', 8)]
The original list 2 : [('key2', 1), ('key1', 4), ('key3', 2)]
The grouped summation tuple list is : [('key1', 8), ('key2', 9), ('key3', 8)]
Time Complexity:
The time complexity of this solution is O(nlogn) because of the sorting operation. The reduce function operation is O(n).
Space Complexity:
The space complexity of this solution is O(n) because we are creating a merged_list and the final output list, both of which can have a maximum of n elements. In addition, there is a small constant space used by the variables for the reduce function.
Similar Reads
Python | Summation of tuples in list
Sometimes, while working with records, we can have a problem in which we need to find the cumulative sum of all the values that are present in tuples. This can have applications in cases in which we deal with a lot of record data. Let's discuss certain ways in which this problem can be solved. Metho
7 min read
Python | Summation of list as tuple attribute
Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the summation of list as tuple attribute. Let's discuss cert
9 min read
Python | Summation of Kth Column of Tuple List
Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the possible accumulation of its Kth index. This problem has applications in the web development domain while working with data information. Let's discuss certain ways in which this task ca
7 min read
Python | Triple List Summation
There can be an application requirement to append elements of 2-3 lists to one list and perform summation. This kind of application has a potential to come into the domain of Machine Learning or sometimes in web development as well. Letâs discuss certain ways in which this particular task can be per
3 min read
Python | Summation of two list of tuples
Sometimes, while working with Python records, we can have a problem in which we need to perform cross-summation of list of tuples. This kind of application is popular in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + zip
6 min read
Python | Column summation of tuples
Sometimes, we encounter a problem where we deal with a complex type of matrix column summation in which we are given a tuple and we need to perform the summation of its like elements. This has a good application in Machine Learning domain. Let's discuss certain ways in which this can be done. Method
7 min read
Python - Cross tuple summation grouping
Sometimes, while working with Python tuple records, we can have a problem in which we need to perform summation grouping of 1st element of tuple pair w.r.t similar 2nd element of tuple. This kind of problem can have application in day-day programming. Let's discuss certain ways in which this task ca
7 min read
Python | Summation of Non-Zero groups
Many times we need to get the summation of not the whole list but just a part of it and at regular intervals. These intervals are sometimes decided statically before traversal, but sometimes, the constraint is more dynamic and hence we require to handle it in more complex way. Criteria discussed her
6 min read
Python - Absolute Tuple Summation
Sometimes, while working with Python tuples, we can have a problem in which we need to perform the summation of absolute values of intermediate tuple elements. This kind of problem can have application in many domains such as web development. Let's discuss certain ways in which this task can be perf
6 min read
Python | Split tuple into groups of n
Given a tuple, the task is to divide it into smaller groups of n. Let's discuss a few methods to accomplish a given task.Method #1: Using enumerate and range function C/C++ Code # Python code to demonstrate # how to split tuple # into the group of k-tuples # initialising tuple ini_tuple = (1, 2, 3,
3 min read