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))
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.
Output:
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.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer