Home » Python » Python programs

# Python program to find the maximum nested tuple

In this program, we are given two tuples. We need to create a Python program to maximize nested Tuples.

Submitted by Shivang Yadav, on November 22, 2021

Extracting fruitful information from data sets is very important when it comes to working with complex collections in python. Program to perform Comparative selection, extraction of stuff based on a certain condition may contribute to your code and make your program work more effectively. In this program, we will find the maximum nested tuple 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)

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

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

Related Programs

- Python program to find the size of a tuple
- Python program for adding a Tuple to List and Vice-Versa
- Python program to find the maximum and minimum K elements in a tuple
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python program to remove all tuples of length K
- Python program to extract digits from tuple list
- Python program to find all pairs combination of two tuples
- Python program to join tuples if similar initial element
- Python program to sort a list of tuples by second item
- Python program to sort a list of tuples in increasing order by the last element in each tuple
- Python program to sort tuples by frequency of their absolute difference
- Python program to remove duplicate tuples irrespective of order
- Python program to order tuples by list
- Python program to concatenate maximum tuples
- Python program to flatten tuple of lists to a tuple
- Python program to change the sign of elements of tuples in a list
- Python program to convert integer values in a list of tuples to float
- Python program to convert set into tuple and tuple into set
- Python program to restrict tuples by frequency of first element's value in a tuple list
- Python program to extract tuples having K digit elements
- Python program to find tuples from list which have all elements divisible by K
- Python program to remove tuples from the list having every element as None
- Python program to assign frequency to tuples
- Python program to check if any list element is present in Tuple
- Python program to remove given character from the first element of Tuple
- Python program to extract tuples with all numeric strings
- Python program to perform tuple intersection in list (order irrespective)
- Python program to convert binary tuple to integer
- Python program to convert tuple matrix to tuple list
- Python program to convert tuple into list by adding the given string after every element
- Python program to sort tuples by their maximum element
- Python program to find tuples with positive elements in list of tuples
- Python program to find the frequency of all tuple elements
- Python program to perform cross pairing in tuple list
- Python program to concatenate tuple elements by delimiter
- Python program to extract all symmetric tuples
- Python program to perform cross tuple summation grouping using 2nd element
- Python program to perform row-wise element addition in Tuple matrix
- Python program to get even indexed elements in tuple
- Python program to sort tuples by total digits
- Python program to remove space between tuple elements
- Python program to print all pair combinations of elements from 2 tuples
- Python program to print all group tuples by Kth index element
- Python program to alternate elements operation on tuple
- Python program to convert Tuple to Tuple Pair
- Python program to extract adjacent coordinates in N dimension
- Python program to perform multiple keys grouped summation
- Python program to convert list of lists to tuple of tuples
- Python program to Flatten Nested Tuples
- Python program to clear tuple elements
- Python program to convert tuple string to integer tuple
- Python program to perform the operation to zip uneven tuples
- Python program to find the sum of tuple elements
- Python program to perform the tuple elements inversions
- Python program to perform union of tuple

- Python program to extract rear elements from tuple string
- Python program to index maximum among tuples
- Python program to perform nested tuple subtraction
- Python program to filter range length tuples
- Python program to perform tuple intersection
- Python program to get records with value at K index
- Python program to perform elementwise AND operation in tuple
- Python program to check if the given tuple is a true record or not
- Python program to concatenate consecutive elements in tuple
- Python program to perform AND operation on tuples
- Python program to remove nested records from tuple
- Python program to sort list of tuples alphabetically
- Python program to find maximum value in record list as tuple attribute
- Python program to find the index of minimum value record
- Python program to extract rear element from list of tuples record
- Python program to find the modulo of tuple elements
- Python program to perform pairwise addition in tuples
- Python program to perform concatenation of two string tuples
- Python program to extract maximum value in record list as tuple attribute
- Python program to find modulo of tuple elements
- Python program to perform division operation on tuples
- Python program to perform XOR operation on tuples
- Python program to raise elements of tuple as a power to another tuple
- Python program to create a tuple from string and list
- Python program for creating N element incremental tuple
- Python program to convert tuple to integer
- Python program to check if the element is present in tuple
- Python program to check if a tuple is a subset of another tuple
- Python program to perform multiplication operation on tuples
- Python program to perform comparison operation on tuples
- Python program to check if the tuple has any none value
- Python program to chunk tuples to N size
- Python program to access front and rear elements from tuple
- Python program to find the maximum element in tuple list
- Python program to add a dictionary to tuple
- Python program to update each element in the tuple list
- Python program to multiply adjacent elements of a tuple
- Python program to extract unique elements in nested tuple
- Python program to perform subtraction of elements of tuples
- Python program to check for None tuple
- Python program to convert tuple to adjacent pair dictionary
- Python program to count all the elements till first tuple
- Python program to perform the addition of nested tuples
- Python program to convert tuple to float value
- Python program to concatenate tuples to nested tuple
- Python program to perform summation of tuple in list
- Python program to flatten tuple list to string
- Python program to repeat tuples N times
- Python program to check if two lists of tuples are identical or not
- Python program to filter tuples according to list element
- Python program to find the maximum difference between tuple pairs
- Python program to record similar tuple occurrence

Comments and Discussions!