Q:

Python | Nested Tuples Subtraction

belongs to collection: Python Tuple Programs

0

Example:

tuple = ("python", "includehelp", 43, 54.23)

Performing Nested Tuple Subtraction

In this program, we are given two nested tuples consisting of integer values. We will be creating a python program that will perform subtraction operations on elements of the nested tuple.

Input:
nestTup1 = ((1, 5), (6, 7), (9, 4))
nestTup2 = ((3, 4), (8, 2), (0, 6))

Output:
((-2, 1), (-2, 5), (9, -2))

For performing this task, we will be traversing both the nested tuples and then for each element of the tuple we will perform individual subtraction for each element and create a nested tuple with resultant values of the same size. In Python programming language, we can perform the task using multiple ways.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1:

One method to perform the task is by using the zip() method to combine elements and then perform the difference operation on the elements using generator expression.

# Python program to perform nested tuple subtraction 
# using a generator expression

# Initialising and printing nested Tuples
tuple1 = ((1, 3), (4, 5), (2, 9), (1, 10))
tuple2 = ((6, 7), (3, 9), (1, 1), (7, 3))
print("The elements of nested tuple 1 : " + str(tuple1))
print("The elements of nested tuple 2 : " + str(tuple2))

# Performing nested Tuple Subtraction 
subsTuple = tuple(tuple(a - b for a, b in zip(tupVal1, tupVal2))\
    for tupVal1, tupVal2 in zip(tuple1, tuple2))

print("The elements of tuple created after Performing nested tuple Subtraction : " + str(subsTuple))

Output:

The elements of nested tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The elements of nested tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The elements of tuple created after Performing nested tuple Subtraction : ((-5, -4), (1, -4), (1, 8), (-6, 7))

Method 2:

Another method to perform the subtraction operation on nested tuples is by using the isinstance() method along with zip() and loop. And to make the work more efficient, we will be using list comprehension.

# Python program to perform nested Tuple Subtraction

def calcDiffOp(tuple1, tuple2):
    if isinstance(tuple1, (list, tuple)) and isinstance(tuple2, (list, tuple)):
        return tuple(calcDiffOp(x, y) for x, y in zip(tuple1, tuple2))
    return tuple1 - tuple2

# Initialising and printing nested Tuples
tuple1 = ((1, 3), (4, 5), (2, 9), (1, 10))
tuple2 = ((6, 7), (3, 9), (1, 1), (7, 3))
print("The elements of nested tuple 1 : " + str(tuple1))
print("The elements of nested tuple 2 : " + str(tuple2))

# Performing nested Tuple Subtraction 
subsTuple = tuple(calcDiffOp(x, y) for x, y in zip(tuple1, tuple2))

print("The elements of tuple created after Performing nested tuple Subtraction : " + str(subsTuple))

Output:

The elements of nested tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The elements of nested tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The elements of tuple created after Performing nested tuple Subtraction : ((-5, -4), (1, -4), (1, 8), (-6, 7))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Filter Range Length Tuples... >>
<< Python | Index Maximum among Tuples...