Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Cross tuple summation grouping
Next article icon

Python | Grouped summation of tuple list

Last Updated : 27 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
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.



Next Article
Python - Cross tuple summation grouping
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python tuple-programs
Practice Tags :
  • python

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 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 | 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 | 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 - 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 - 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
  • Python - Index Value Summation List
    To access the elements of lists, there are various methods. But sometimes we may require to access the element along with the index on which it is found and compute its summation, and for that, we may need to employ different strategies. This article discusses some of those strategies. Method 1: Nai
    4 min read
  • Python - Group list of tuples to dictionary
    The task is to convert a list of tuples into a dictionary, where each tuple consists of two elements. The first element of each tuple becomes the key and the second element becomes the value. If a key appears multiple times, its corresponding values should be grouped together, typically in a list. F
    4 min read
  • Python - Tuple Matrix Columns Summation
    Sometimes, while working with Tuple Matrix, we can have a problem in which we need to perform summation of each column of tuple matrix, at the element level. This kind of problem can have application in Data Science domains. Let's discuss certain ways in which this task can be performed. Input : tes
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences