Python program to perform the addition of nested tuples

In this problem, we are given two nested tuples consisting of integer values. Our task is to create a Python program to perform the addition of Nested Tuples.
Submitted by Shivang Yadav, on January 07, 2022

Performing basic operations on nested collection in Python is required while implementing the programs in high-level programs like A.I. Here, we will see a Python program to perform the addition operation on elements of nested tuples.

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)

Addition of Nested Tuple

In this article, we are given two nested tuples consisting of integer values. Our task is to create a program that will add the elements of the tuples.

Input:
tup1 = ((5, 1), (8, 9), (2, 6))
tup2 = ((3, 4), (5, 6), (7, 8))

Output:
((8, 5), (13, 15), (9, 14))

To perform this task we will loop over the nested tuples and add them. This task can be performed using various methods in Python.

Method 1:

One method to solve the problem is by adding each individual value of the tuple by looping over it and creating a nested collection using the zip() method.

# Initializing and printing nested tuples

tup1 = ((5, 1), (8, 9), (2, 6))
tup2 = ((3, 4), (5, 6), (7, 8))

print("The elements of nested Tuple 1 : " + str(tup1))
print("The elements of nested Tuple 2 : " + str(tup2))
  
# Performing the addition of nested tuples 
res = tuple(tuple(a + b for a, b in zip(myTup1, myTup2))\
      for myTup1, myTup2 in zip(tup1, tup2))

print("The tuple created after adding nested Tuples : " + str(res))

Output:

The elements of nested Tuple 1 : ((5, 1), (8, 9), (2, 6))
The elements of nested Tuple 2 : ((3, 4), (5, 6), (7, 8))
The tuple created after adding nested Tuples : ((8, 5), (13, 15), (9, 14))

Method 2:

Another method to solve the problem is by using checking for nested tuples using the isinstance() method and then adding the elements. Using this method, we will be able to add elements of the nested tuples which have more than one level of nested collection.

# Python program to perform the addition 
# of nested tuples

def findTupleSum(tuple1, tuple2):
    if isinstance(tuple1, (list, tuple)) and isinstance(tuple2, (list, tuple)):
       return tuple(findTupleSum(x, y) for x, y in zip(tuple1, tuple2))
    return tuple1 + tuple2  
  
# Initializing and printing nested tuples
tup1 = ((5, 1), (8, 9), (2, 6))
tup2 = ((3, 4), (5, 6), (7, 8))

print("The elements of nested Tuple 1 : " + str(tup1))
print("The elements of nested Tuple 2 : " + str(tup2))
  
# Performing the addition of nested tuples 
res = tuple(findTupleSum(x, y) for x, y in zip(tup1, tup2))

print("The tuple created after adding nested Tuples : " + str(res))

Output:

The elements of nested Tuple 1 : ((5, 1), (8, 9), (2, 6))
The elements of nested Tuple 2 : ((3, 4), (5, 6), (7, 8))
The tuple created after adding nested Tuples : ((8, 5), (13, 15), (9, 14))

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






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