Example:
tuple = ("python", "includehelp", 43, 54.23)
Performing subtraction of elements of tuples
In this problem, we are given two tuples and we need to create a Python program that will create a tuple that contains the result of subtraction of elements of the tuples.
Input:
(3, 1, 7, 4), (6, 5, 3, 2)
Output:
(-3, -4, 4, 2)
To perform elementwise subtraction of tuples, we will be a loop over the tuples with the same iterator and then perform the subtraction for each element and return the resultant values.
Method 1:
One method to solve the problem is by using a map() to map the subtraction of elements of both tuples using the lambda function with subtraction operation. Then convert the result of this map() function to a tuple using tuple() method.
Output:
Method 2:
Another method to solve the problem is by using the sub() method from the operator library in Python. We will be using the map() to map the elements of two tuples using subtraction. The lambda function here will be using operator.sub to perform subtraction.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer