Open In App

Python | Grouped summation of tuple list

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# group summation of tuple list
# using Counter() + "+" operator
from collections import Counter
 
# initializing list of tuples
test_list1 = [('key1', 4), ('key3', 6), ('key2', 8)]
test_list2 = [('key2', 1), ('key1', 4), ('key3', 2)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using Counter() + "+" operator
# group summation of tuple list
cumul_1 = Counter(dict(test_list1))
cumul_2 = Counter(dict(test_list2))
cumul_3 = cumul_1 + cumul_2
res = list(cumul_3.items())
 
# print result
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




# Python3 code to demonstrate
# group summation of tuple list
# Initialize an empty dictionary to store the results
results = {}
# initializing list of tuples
test_list1 = [('key1', 4), ('key3', 6), ('key2', 8)]
test_list2 = [('key2', 1), ('key1', 4), ('key3', 2)]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Iterate over the tuples in the list
for key, value in test_list1 + test_list2:
    # If the key is already in the dictionary, add the value to the existing one
    if key in results:
        results[key] += value
    # If the key is not in the dictionary, add it with the corresponding value
    else:
        results[key] = value
 
# Convert the dictionary to a list of tuples
res = list(results.items())
# print result
print("The grouped summation tuple list is : " + str(res))
 
# This code is contributed by Edula Vinay Kumar Reddy


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
 
# initializing list of tuples
test_list1 = [('key1', 4), ('key3', 6), ('key2', 8)]
test_list2 = [('key2', 1), ('key1', 4), ('key3', 2)]
 
# using defaultdict and for loop
dict_sum = defaultdict(int)
for k, v in test_list1 + test_list2:
    dict_sum[k] += v
 
# converting the dictionary to list of tuples
res = list(dict_sum.items())
 
# print result
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)]
 
# Merge the two lists and sort by key
merged_list = sorted(test_list1 + test_list2, key=lambda x: x[0])
 
# Group the tuples by key and calculate the sum of values for each group
grouped = [(key, sum(value for _, value in group))
           for key, group in groupby(merged_list, key=lambda x: x[0])]
 
# Print the result
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:

  1. Combine the two lists of tuples, test_list1 and test_list2.
  2. Create a set of unique keys present in the combined list of tuples.
  3. For each unique key, use a dictionary comprehension to sum up the values of all tuples in the combined list with the same key.
  4. Convert the resulting dictionary into a list of tuples.
  5. 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:

  1. Import the pandas library.
  2. Convert both lists to pandas dataframes, with the first element of each tuple as the index and the second element as the value.
  3. Concatenate the dataframes using the “+” operator, grouping by the index and summing the values.
  4. Convert the resulting dataframe to a list of tuples using the “to_records()” method.

Python3




# Python3 code to demonstrate
# group summation of tuple list
# using pandas library
 
import pandas as pd
 
# initializing list of tuples
test_list1 = [('key1', 4), ('key3', 6), ('key2', 8)]
test_list2 = [('key2', 1), ('key1', 4), ('key3', 2)]
 
# converting lists to dataframes
df1 = pd.DataFrame(test_list1, columns=["key", "value"]).set_index("key")
df2 = pd.DataFrame(test_list2, columns=["key", "value"]).set_index("key")
 
# concatenating dataframes and grouping by index
df3 = pd.concat([df1, df2]).groupby(level=0).sum()
 
# converting dataframe to list of tuples
res = list(df3.to_records())
 
# print result
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:

  1. Initialize two test lists, test_list1 and test_list2, each containing tuples of key-value pairs.
  2. Merge the two lists into a single list, merged_list, using the concatenation operator (+).
  3. 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.
  4. 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.
  5. 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.
  6. 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)]
 
# Merge the two lists and sort by key
merged_list = sorted(test_list1 + test_list2, key=lambda x: x[0])
 
# Group the tuples by key and calculate the sum of values for each group
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 result
print("The grouped summation tuple list is : " + str(grouped))
#This code is contributed by Rayudu.


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:

  1. Concatenate the two input lists into a single list.
  2. Sort the list using the key as the first element of each tuple.
  3. Apply reduce function to the sorted list.
  4. 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.
  5. 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)]
# printing original lists
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))
#This code is contributed by Pushpa.


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.



Last Updated : 27 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads