Python | Grouped summation of tuple list

Many times, we are given a list of tuples and we need to group its keys and perform certain operation while grouping. The most common operation being 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 : Using Counter() + "+" operator

This task can be performed using the Counter function as it internally groups and addition operator can be used to specify the functionality on the grouped result.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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)]

 



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.