Python | Aggregate values by tuple keys

Sometimes, while working with records, we can have a problem in which we need to group the like keys and aggregate the values of like keys. This can have application in any kind of scoring. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using Counter() + generator expression
The combination of above functions can be used to perform this particular task. In this, we need to first combine the like key elements and task of aggregation is performed by Counter().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Aggregate values by tuple keys
# using Counter() + generator expression
from collections import Counter
  
# initialize list
test_list = [('gfg', 50), ('is', 30), ('best', 100), 
                          ('gfg', 20), ('best', 50)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Aggregate values by tuple keys
# using Counter() + generator expression
res = list(Counter(key for key, num in test_list 
                  for idx in range(num)).items())
  
# printing result
print("List after grouping : " + str(res))

chevron_right


Output :



The original list is : [('gfg', 50), ('is', 30), ('best', 100), ('gfg', 20), ('best', 50)]
List after grouping : [('best', 150), ('gfg', 70), ('is', 30)]

 

Method #2 : Using groupby() + map() + itemgetter() + sum()
The combination of above functions can also be used to perform this particular task. In this, we group the elements using groupby(), decision of key’s index is given by itemgetter. Task of addition(aggregation) is performed by sum() and extension of logic to all tuples is handled by map().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Aggregate values by tuple keys
# using groupby() + map() + itemgetter() + sum()
from itertools import groupby
from operator import itemgetter
  
# initialize list
test_list = [('gfg', 50), ('is', 30), ('best', 100),
                          ('gfg', 20), ('best', 50)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Aggregate values by tuple keys
# using groupby() + map() + itemgetter() + sum()
res = [(key, sum(map(itemgetter(1), ele)))
       for key, ele in groupby(sorted(test_list, key = itemgetter(0)), 
                                                key = itemgetter(0))]
  
# printing result
print("List after grouping : " + str(res))

chevron_right


Output :

The original list is : [('gfg', 50), ('is', 30), ('best', 100), ('gfg', 20), ('best', 50)]
List after grouping : [('best', 150), ('gfg', 70), ('is', 30)]



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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.