Change language

Python | Aggregate values ​​across tuple keys

|

Method # 1: Using ` Counter () ` + Expression Generator
A combination of the above functions can be used to accomplish this specific task. In this we need to first combine the same key elements and the aggregation task is performed by ` Counter () `.

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

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 () `
Combination the above functions can also be used to accomplish this specific task. In doing so, we group the items using groupby (), the key index solution is given by itemgetter. The task of addition (aggregation) is performed by the sum () function, and the expansion of the logic for all tuples — map ().

 ` # Python3 code to demonstrate how it works ` ` # Aggregate values ​​by tuple keys ` ` # using groupby () + map () + itemgetter () + sum () ` ` from ` ` itertools ` ` import ` ` groupby ` ` from ` ` operator ` ` import ` ` itemgetter `   ` # initialize the list ` ` test_list ` ` = ` ` [(` ` ’ gfg’ ` `, ` ` 50 ` `), (` ` ’is’ ` `, ` ` 30 ` `), (` ` ’best’ ` `, ` ` 100 ` `), ` ` ` ` (` `’ gfg’ ` `, ` ` 20 ` `), (` ` ’best’ ` `, ` ` 50 ` `)] `   ` # print original list ` ` print ` ` (` ` "The original list is:" ` ` + ` ` str ` ` (test_list)) ` ` `  ` # Aggregate values ​​by key am tuple ` ` # 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 ` `))] `   ` # print result ` ` print ` ` (` ` "List after grouping:" ` ` + ` ` str ` ` (res)) `

Output:

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

Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries