Home »
Python »
Python programs
Python program to perform subtraction of elements of tuples
In this program, we are given a tuple of elements. We need to create a Python program to perform the subtraction of elements of tuples.
Submitted by Shivang Yadav, on January 04, 2022
Python programming language has found wide usage in multiple advanced engineering fields. This is due to the incorporation of multiple utility programs that can be used to perform functions that are required by large programs to function properly. Working with collections and performing a certain operation on them is quite common. Here, we will see a Python program that will perform the subtraction of elements of the tuple.
Before going further with the problem, let's recap some basic topics that will help in understanding the solution.
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.
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.
# Initialing and printing
# the two tuples
myTup1 = (3, 1, 7, 4)
myTup2 = (6, 5, 3, 2)
print("The elements of tuple 1 are " + str(myTup1))
print("The elements of tuple 2 are " + str(myTup2))
# Performing Subtraction operation
# on Tuples
subsTup = tuple(map(lambda i, j: i - j, myTup1, myTup2))
print("The elements of Subtraction tuple are " + str(subsTup))
Output:
The elements of tuple 1 are (3, 1, 7, 4)
The elements of tuple 2 are (6, 5, 3, 2)
The elements of Subtraction tuple are (-3, -4, 4, 2)
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.
# Python program to perform subtraction
# of elements of tuples
import operator
# Initialing and printing the
# two tuples
myTup1 = (3, 1, 7, 4)
myTup2 = (6, 5, 3, 2)
print("The elements of tuple 1 are " + str(myTup1))
print("The elements of tuple 2 are " + str(myTup2))
# Performing Subtraction operation
# on Tuples
subsTup = tuple(map(operator.sub, myTup1, myTup2))
print("The elements of Subtraction tuple are " + str(subsTup))
Output:
The elements of tuple 1 are (3, 1, 7, 4)
The elements of tuple 2 are (6, 5, 3, 2)
The elements of Subtraction tuple are (-3, -4, 4, 2)
Python Tuple Programs »