ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.