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 | Combining tuples in list of tuples
Next article icon

Python – Combinations of sum with tuples in tuple list

Last Updated : 05 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes, while working with data, we can have a problem in which we need to perform tuple addition among all the tuples in list. This can have applications in many domains. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using combinations() + list comprehension

This problem can be solved using combinations of the above functions. In this, we use combinations() to generate all possible combinations among tuples and list comprehension is used to feed addition logic. 

Python3




# Python3 code to demonstrate working of
# Summation combination in tuple lists
# Using list comprehension + combinations
from itertools import combinations
 
# Initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10)]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Summation combination in tuple lists
# Using list comprehension + combinations
res = [(b1 + a1, b2 + a2) for (a1, a2), (b1, b2) in combinations(test_list, 2)]
 
# Printing result
print("The Summation combinations are : " + str(res))
 
 
Output : 
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)] The Summation combinations are : [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)]

Time complexity: O(n^2) where n is the length of the input list.
Auxiliary space: O(n) where n is the length of the input list.

Method #2 : Using list comprehension + zip() + operator.add + combinations() 

The combinations of the above methods can also solve this problem. In this, we perform the task of addition using add() and the like indexed elements are linked using zip() function. 

Python3




# Python3 code to demonstrate working of
# Summation combination in tuple lists
# Using list comprehension + zip() + operator.add + combinations()
 
from itertools import combinations
import operator
 
# Initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10)]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Summation combination in tuple lists
# Using list comprehension + zip() + operator.add + combinations()
res = [(operator.add(*a), operator.add(*b))
       for a, b in (zip(y, x) for x, y in combinations(test_list, 2))]
 
# Printing result
print("The Summation combinations are : " + str(res))
 
 
Output : 
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)] The Summation combinations are : [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)]

Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the list comprehension + zip() + operator.add + combinations() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

Method #3: Using nested for loops

Use nested for loops to iterate over the list and add the elements of each tuple to find the summation combination

Python3




# Python3 code to demonstrate working of
# Summation combination in tuple lists
# Using nested for loops
 
# Initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10)]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Summation combination in tuple lists
# Using nested for loops
res = []
for i in range(len(test_list)):
    for j in range(i+1, len(test_list)):
        res.append((test_list[i][0]+test_list[j][0],
                    test_list[i][1]+test_list[j][1]))
 
# Printing result
print("The Summation combinations are : " + str(res))
 
 
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)] The Summation combinations are : [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)]

Time complexity: O(n^2), where n is the length of the input list. 
Auxiliary space: O(n^2), where n is the length of the input list. 

Method #4: Using itertools.combinations() + map() + lambda function

  1. Import itertools module’s combinations() function, which generates all possible combinations of elements of the input iterable
  2. Apply map() function with a lambda function to calculate the sum of each tuple in the generated combinations
  3. Store the resulting tuples in a list.

Python3




import itertools
 
# Input list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10)]
 
# Printing input list for understanding
print("The original list : " + str(test_list))
 
res = list(map(lambda x: (x[0][0]+x[1][0], x[0][1] +
                          x[1][1]), itertools.combinations(test_list, 2)))
 
# Printing the resultant list
print("The Summation combinations are : " + str(res))
 
 
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)] The Summation combinations are : [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)]

Time complexity: O(n^2) (because we need to generate all possible combinations of size 2 from n elements)
Auxiliary space: O(n^2) (because we need to store all the generated tuples in a list)



Next Article
Python | Combining tuples in list of tuples
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python tuple-programs
Practice Tags :
  • python

Similar Reads

  • Python | Combining tuples in list of tuples
    Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways
    7 min read
  • Python - Join Tuples to Integers in Tuple List
    Sometimes, while working with Python records, we can have a problem in which we need to concatenate all the elements, in order, to convert elements in tuples in List to integer. This kind of problem can have applications in many domains such as day-day and competitive programming. Let's discuss cert
    5 min read
  • Python - Convert List of Lists to Tuple of Tuples
    Sometimes, while working with Python data, we can have a problem in which we need to perform interconversion of data types. This kind of problem can occur in domains in which we need to get data in particular formats such as Machine Learning. Let us discuss certain ways in which this task can be per
    8 min read
  • Flatten tuple of List to tuple - Python
    The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3). Using itertools.chain(
    3 min read
  • Combinations of Elements till size N in List - Python
    We are given a list and our task is to generate all possible combinations of its elements up to a given size N, including all lengths from 1 to N. For example, if we have a = [1, 2, 3] and N = 2 then the output should be [(1,), (2,), (3,), (1, 2), (1, 3), (2, 3)], where we generate all subsets of si
    3 min read
  • Python | Count tuples occurrence in list of tuples
    Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using It
    5 min read
  • Python - Pairs with Sum equal to K in tuple list
    Sometimes, while working with data, we can have a problem in which we need to find the sum of pairs of tuple list. And specifically the sum that is equal to K. This kind of problem can be important in web development and competitive programming. Lets discuss certain ways in which this task can be pe
    6 min read
  • Generating a "Set Of Tuples" from A "List of Tuples" - Python
    We are given a list of tuples and we need to extract only the unique tuples while removing any duplicates. This is useful in scenarios where you want to work with distinct elements from the list. For example:We are given this a list of tuples as [(1, 2), (3, 4), (1, 2), (5, 6)] then the output will
    3 min read
  • Python | Find all triplets in a list with given sum
    Given a list of integers, write a Python program to find all triplets that sum up to given integer 'k'. Examples: Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10 Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)] Input : [12, 3, 6, 1, 6, 9], k = 24 Output : [(12, 6, 6), (12, 9, 3)] Approach #1 :
    4 min read
  • Python | Group tuples in list with same first value
    Given a list of tuples, the task is to print another list containing tuple of same first element. Below are some ways to achieve above tasks. Example: Input : [('x', 'y'), ('x', 'z'), ('w', 't')] Output: [('w', 't'), ('x', 'y', 'z')] Method #1: Using extend C/C++ Code # Python code to find common #
    7 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