Q:

Python program to find the maximum nested tuple

belongs to collection: Python Tuple Programs

0

Example:

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

Finding the Maximum Nested Tuple

To find the maximum nested tuple, we will loop through both the tuple and for each nested tuple element find the maximum of each and create a nested collection similar to the initial one.

Input:
tuple1 = ((1, 6), (2, 5), (8, 9))
tuple2 = ((3, 4), (5, 7), (9, 3))

Output:
((3, 6), (5, 7), (9, 9))

In Python, we can perform the task in multiple ways using one of the multiple methods that are present in the function.

All Answers

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

Method 1:

One method to solve the problem is by finding the maximum of elements zipped together using the zip() method using the max() method. All the logic is put together using the generator expression.

# Python program to find the Maximum Nested Tuple

# Initialize tuples
# Initializing and printing initial Tuples
tuple1 = ((1, 6), (2, 5), (8, 9))
tuple2 = ((3, 4), (5, 7), (9, 3))
print("The values of Tuple 1 are " + str(tuple1))
print("The values of Tuple 2 are " + str(tuple2))

# Finding maximum nested Tuples 
maxTuple = tuple(tuple(max(val1, val2) for val1, val2 in zip(tup1, tup2))\
	for tup1, tup2 in zip(tuple1, tuple2))

# Printing the maximum nested Tuple 
print("The values of tuple after maximization : " + str(maxTuple))

Output:

The values of Tuple 1 are ((1, 6), (2, 5), (8, 9))
The values of Tuple 2 are ((3, 4), (5, 7), (9, 3))
The values of tuple after maximization : ((3, 6), (5, 7), (9, 9))

Method 2:

An alternate method to solve the problem is by checking for the nesting type of the tuple using the instance() method. Then for each instance, we will be finding the maximum elements of the same index for both the tuples using max() and zip() putting them together using list comprehension.

# Python program to find the Maximum Nested Tuple

# Utility function to find maximum Nested Tuple 
def findMax(tup1, tup2):
    if isinstance(tup1, (list, tuple)) and isinstance(tup2, (list, tuple)):
        return tuple(findMax(x, y) for x, y in zip(tup1, tup2))
    return max(tup1, tup2)
    
# Initialize tuples
# Initializing and printing initial Tuples
tuple1 = ((1, 6), (2, 5), (8, 9))
tuple2 = ((3, 4), (5, 7), (9, 3))
print("The values of Tuple 1 are " + str(tuple1))
print("The values of Tuple 2 are " + str(tuple2))

# Finding maximum nested Tuples 
maxTuple = tuple(findMax(x, y) for x, y in zip(tuple1, tuple2))

# Printing the maximum nested Tuple 
print("The values of tuple after maximization : " + str(maxTuple))

Output:

The values of Tuple 1 are ((1, 6), (2, 5), (8, 9))
The values of Tuple 2 are ((3, 4), (5, 7), (9, 3))
The values of tuple after maximization : ((3, 6), (5, 7), (9, 9))

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 program to remove nested records from tuple... >>
<< Python program to perform AND operation on tuples...