Open In App

Python | How to get Subtraction of tuples

Improve
Improve
Like Article
Like
Save
Share
Report

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))


Output

The 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}")


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 tuples.



Last Updated : 04 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads