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 | Nested Tuples Subtraction
Next article icon

Python | Mutual tuple subtraction in list

Last Updated : 13 Apr, 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 subtraction among all the tuples in list. This can have application 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 above functions. In this, we use combinations() to generate all possible combination among tuples and list comprehension is used to feed subtraction logic. 

Python3




# Python3 code to demonstrate working of
# Mutual tuple subtraction in list
# 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))
 
# Mutual tuple subtraction in list
# Using list comprehension + combinations
res = [(b1 - a1, b2 - a2) for (a1, a2), (b1, b2) in combinations(test_list, 2)]  
 
# printing result
print("The mutual subtraction tuples are : " + str(res))
 
 
Output : 
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)] The mutual subtraction tuples are : [(4, 3), (3, -3), (4, 6), (-1, -6), (0, 3), (1, 9)]

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

  Method #2 : Using list comprehension + zip() + operator.sub + combinations() The combinations of above methods can also solve this problem. In this, we perform the task of subtraction using sub() and the like indexed elements are linked using zip(). 

Python3




# Python3 code to demonstrate working of
# Mutual tuple subtraction in list
# Using list comprehension + zip() + operator.sub + 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))
 
# Mutual tuple subtraction in list
# Using list comprehension + zip() + operator.sub + combinations()
res = [(operator.sub(*a), operator.sub(*b))\
      for a, b in (zip(y, x) for x, y in combinations(test_list, 2))]
 
# printing result
print("The mutual subtraction tuples are : " + str(res))
 
 
Output : 
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)] The mutual subtraction tuples are : [(4, 3), (3, -3), (4, 6), (-1, -6), (0, 3), (1, 9)]

 Using nested for loops:

Approach:

Initialize an empty list result.
Using a list comprehension with two nested loops, iterate over all pairs of tuples in the input list, except pairs of identical tuples.
For each pair of tuples, subtract the first element of the second tuple from the first element of the first tuple, and subtract the second element of the second tuple from the second element of the first tuple.
Append the resulting tuple to result.
Return result.

Python3




def subtract_tuples(tuples):
    result = []
    for t1 in tuples:
        for t2 in tuples:
            if t1 != t2:
                result.append((t1[0] - t2[0], t1[1] - t2[1]))
    return result
 
tuples = [(1, 2), (3, 4), (5, 6)]
print(subtract_tuples(tuples))
# Output: [(-2, -2), (-4, -4), (2, 2), (4, 4), (0, 0), (-2, -2), (2, 2), (0, 0), (4, 4)]
 
 
Output
[(-2, -2), (-4, -4), (2, 2), (-2, -2), (4, 4), (2, 2)]

Time Complexity: O(n^2)
Auxiliary Space: O(n^2)



Next Article
Python | Nested Tuples Subtraction
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Nested Tuples Subtraction
    Sometimes, while working with records, we can have a problem in which we require to perform index-wise subtraction of tuple elements. This can get complicated with tuple elements being tuples and inner elements again being tuple. Let’s discuss certain ways in which this problem can be solved.  Metho
    7 min read
  • 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 | Nth tuple index Subtraction by K
    Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Let’s discuss certain ways in which K can be subtracted to Nth element of tuple in list. Method #1 : Using loop Using loops this t
    6 min read
  • Python - Subtract K from tuples list
    Sometimes, while working with data, we can have a problem in which we need to perform update operation on tuples. This can have application across many domains such as web development. Let’s discuss certain ways in which subtraction of K can be performed. Method #1 : Using list comprehension This is
    4 min read
  • Sort Tuple of Lists in Python
    The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
    3 min read
  • Python | Sort lists in tuple
    Sometimes, while working with Python tuples, we can have a problem in which we need to sort the tuples which constitutes of lists and we need to sort each of them. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + sorted() + generator expression This task ca
    4 min read
  • 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 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 cha
    10 min read
  • Python | How to get Subtraction of tuples
    Sometimes, while working with records, we might have a common problem of subtracting the contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let’s discuss certain ways in which this task can be perform
    5 min read
  • Python - List of tuples to multiple lists
    Converting a list of tuples into multiple lists involves separating the tuple elements into individual lists. This can be achieved using methods like zip(), list comprehensions or loops, each offering a simple and efficient way to extract and organize the data. Using zip()zip() function is a concise
    3 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
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