Python | Nested Tuples Subtraction

In this program, we are given two nested tuples consisting of integer elements. We need to create a Python program to perform nested tuple subtraction.
Submitted by Shivang Yadav, on November 08, 2021

Nested Collection operations are a bit trickier to implement while working with nested collections. We need to be extra cautious in order to avoid errors or unexpected output. Here, we will be performing subtraction on nested tuples in Python.

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

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

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.