Python | How to get Subtraction of tuples
Last Updated :
04 Apr, 2023
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 performed.
Method #1: Using map() + lambda Combination of the above functionalities can solve the problem for us. In this, we compute the subtraction using lambda functions and extend the logic to keys using map().
Python3
# Python3 code to demonstrate working of
# Subtraction of tuples
# using map() + lambda
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Subtraction of tuples
# using map() + lambda
res = tuple(map(lambda i, j: i - j, test_tup1, test_tup2))
# printing result
print("Resultant tuple after subtraction : " + str(res))
Output : The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after subtraction : (8, -1, -13)
Time complexity: O(n) where n is the length of the tuples
Auxiliary space: O(n) where n is the length of the resultant tuple
Method #2: Using map() + sub() The combination of above functions can help us achieve this task. In this, we first extend the logic to all using map() and then perform subtraction of each index using sub().
Python3
# Python3 code to demonstrate working of
# Addition of tuples
# using map() + sub()
import operator
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Addition of tuples
# using map() + sub()
res = tuple(map(operator.sub, test_tup1, test_tup2))
# printing result
print("Resultant tuple after subtraction : " + str(res))
Output : The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after subtraction : (8, -1, -13)
Time complexity: O(n), where n is the length of the tuples (assuming the time complexity of the map() function and the operator.sub() function is constant).
Auxiliary Space: O(n), where n is the length of the tuples (as the result is stored in a new tuple of the same length as the input tuples).
Method #3 : Using for loop and tuple() method
Python3
# Python3 code to demonstrate working of
# Subtraction of tuples
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Subtraction of tuples
res=[]
for i in range(0,len(test_tup1)):
res.append(test_tup1[i]-test_tup2[i])
res=tuple(res)
# printing result
print("Resultant tuple after subtraction : " + str(res))
OutputThe original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after subtraction : (8, -1, -13)
Method #4: Using numpy.subtract()
Note: Install numpy module using command "pip install numpy"
This method will use numpy library and numpy.subtract function to subtract the elements of both tuples at corresponding index. It will return the subtracted tuple.
Python3
import numpy as np
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Subtraction of tuples using numpy
res = tuple(np.subtract(test_tup1, test_tup2))
# printing result
print("Resultant tuple after subtraction : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
Output:
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after subtraction : ( 8 -1 -13)
Time complexity: O(n)
Auxiliary Space: O(n) where n is the length of the tuples.
Using zip:
Approach:
Define the function tuple_subtraction with two parameters t1 and t2.
Create a list comprehension that subtracts the corresponding elements of the tuples using the zip function.
Convert the resulting list to a tuple using the tuple() function and return it.
Create two tuples t1 and t2 with values (10, 4, 5) and (2, 5, 18), respectively.
Call the tuple_subtraction function with t1 and t2 as arguments and assign the result to a variable result.
Print the original tuples and the resulting tuple using formatted strings.
Python3
def tuple_subtraction(t1, t2):
return tuple(a - b for a, b in zip(t1, t2))
t1 = (10, 4, 5)
t2 = (2, 5, 18)
result = tuple_subtraction(t1, t2)
print(f"The original tuple 1: {t1}")
print(f"The original tuple 2: {t2}")
print(f"Resultant tuple after subtraction: {result}")
OutputThe original tuple 1: (10, 4, 5)
The original tuple 2: (2, 5, 18)
Resultant tuple after subtraction: (8, -1, -13)
Time complexity: O(n), where n is the length of the tuples.
Auxiliary Space: O(n), where n is the length of the tuples.
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 | 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 | Mutual tuple subtraction in list 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
3 min read
How To Slice A List Of Tuples In Python? In Python, slicing a list of tuples allows you to extract specific subsets of data efficiently. Tuples, being immutable, offer a stable structure. Use slicing notation to select ranges or steps within the list of tuples. This technique is particularly handy when dealing with datasets or organizing i
3 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