Python - Maximize Nested Tuples
Last Updated :
06 Apr, 2023
Sometimes, while working with records, we can have a problem in which we require to perform an index-wise maximum of tuple elements. This can get complicated with tuple elements being tuples and inner elements again being tuples. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using zip() + max() + nested generator expression
The combination of the above functions can be used to perform the task. In this, we combine the elements across tuples using zip(). The iterations and maximize logic are provided by the generator expression.
Python3
# Python3 code to demonstrate working of
# Maximizing Nested Tuples
# using zip() + nested generator expression + max()
# initialize tuples
test_tup1 = ((1, 3), (4, 5), (2, 9), (1, 10))
test_tup2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Maximizing Nested Tuples
# using zip() + nested generator expression + max()
res = tuple(tuple(max(a, b) for a, b in zip(tup1, tup2))
for tup1, tup2 in zip(test_tup1, test_tup2))
# printing result
print("The resultant tuple after maximization : " + str(res))
Output : The original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after maximization : ((6, 7), (4, 9), (2, 9), (7, 10))
Method #2: Using isinstance() + zip() + max() + loop + list comprehension
The combination of the above functions can be used to perform this particular task. In this, we check for the nesting type and perform recursion. This method can give flexibility of more than 1 level nesting.
Python3
# Python3 code to demonstrate working of
# Maximizing Nested Tuples
# using isinstance() + zip() + loop + list comprehension + max()
# function to perform task
def tup_max(tup1, tup2):
if isinstance(tup1, (list, tuple)) and isinstance(tup2, (list, tuple)):
return tuple(tup_max(x, y) for x, y in zip(tup1, tup2))
return max(tup1, tup2)
# initialize tuples
test_tup1 = ((1, 3), (4, 5), (2, 9), (1, 10))
test_tup2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Maximizing Nested Tuples
# using isinstance() + zip() + loop + list comprehension + max()
res = tuple(tup_max(x, y) for x, y in zip(test_tup1, test_tup2))
# printing result
print("The resultant tuple after maximization : " + str(res))
Output : The original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after maximization : ((6, 7), (4, 9), (2, 9), (7, 10))
Time complexity: O(n), where 'n' is the length of the tuples.
Auxiliary space: O(n), where 'n' is the length of the tuples.
Method #3: Using map+lambda
Approach: Use the map() function to apply a lambda function to each pair of corresponding elements in the input tuples.
The lambda function takes two tuples as input and uses the map() function with the max() function to create a new tuple with the maximum elements from each pair of nested tuples. The map() function returns an iterator over the resulting tuples, which we convert to a tuple using the tuple() function.
Algorithm:
- Use the `map()` function with a lambda function to apply the `max()` function to each pair of corresponding elements in the input tuples.
- The lambda function takes two tuples as input, and uses the `map()` function with the `max()` function to create a new tuple with the maximum elements from each pair of nested tuples.
- Convert the resulting iterator over tuples to a tuple using the `tuple()` function.
- Return the tuple of maximized nested tuples.
Python3
# Define the two input tuples
tuple1 = ((1, 3), (4, 5), (2, 9), (1, 10))
tuple2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# Use the map function and lambda function to create a new tuple with the maximum elements
# The lambda function takes two tuples as input, and uses the map function with the max function to create a new tuple with the maximum elements
# The map function applies the lambda function to each pair of corresponding elements in the input tuples
# Finally, the result is converted to a tuple using the tuple function
max_tuple = tuple(map(lambda t1, t2: tuple(map(max, t1, t2)), tuple1, tuple2))
# Print the maximized tuple
print(max_tuple)
Output((6, 7), (4, 9), (2, 9), (7, 10))
Time complexity: O(n)
Where n is the length of the input tuples. This is because the lambda function takes two tuples as input and applies the map function with the max function to each pair of corresponding elements in the input tuples. Since each tuple has a length of two, the map function with the max function takes constant time. Therefore, the time complexity of the lambda function is O(1). The map function applies the lambda function to each pair of corresponding elements in the input tuples, which takes O(n) time, where n is the length of the input tuples. Finally, the tuple function creates a new tuple with the same length as the input tuples, which also takes O(n) time. Therefore, the overall time complexity of this code is O(n).
Auxiliary Space: O(n)
Where n is the length of the input tuples. This is because the map function and lambda function create a new tuple with the same length as the input tuples, and the resulting tuple is stored in memory. The space complexity of the map function and lambda function is O(n), where n is the length of the input tuples. The space complexity of the tuple function is also O(n), since it creates a new tuple with the same length as the input tuples. Therefore, the overall space complexity of this code is O(n).
Method 4: Using the built-in function map() along with a lambda function
This program initializes two tuples test_tup1 and test_tup2, applies the max() function to corresponding elements of the tuples using map() and lambda, and converts the resulting list of tuples to a tuple of tuples using tuple(). The program then prints the original and resulting tuples.
- Define the two tuples to be maximized: test_tup1 and test_tup2.
- Apply the map() function to the zip() of test_tup1 and test_tup2, with a lambda function that applies max() to each pair of corresponding elements. This generates a list of tuples with the maximum values.
- Convert the resulting list of tuples to a tuple of tuples using the tuple() function.
- Print the original tuples and the resulting tuple after maximization.
Python3
# initialize tuples
test_tup1 = ((1, 3), (4, 5), (2, 9), (1, 10))
test_tup2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# apply max() to corresponding elements using map() and lambda
max_tuples = map(lambda tup: tuple(map(max, *tup)), zip(test_tup1, test_tup2))
# convert list of tuples to tuple of tuples
res = tuple(max_tuples)
# print original and resulting tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
print("The resultant tuple after maximization : " + str(res))
OutputThe original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after maximization : ((6, 7), (4, 9), (2, 9), (7, 10))
Time complexity of this approach is O(nm)
where n is the length of the tuples and m is the length of the sub-tuples.
Auxiliary space complexity is O(nm)
As a new tuple of tuples is created to store the resulting values.
Method #5: Using recursion
Here's another approach to solve the problem using recursion. This method works by recursively comparing the nested tuples in the input tuples and returning the maximum values.
Step-by-step approach:
- Define a function max_nested_tuple that takes two tuples as input parameters.
- In the function, check if both the input parameters are tuples or not using the isinstance() method. If not, return the maximum value.
- If both parameters are tuples, initialize an empty list to store the maximum values.
- Loop through each tuple element in the input tuples using the zip() method and recursively call the max_nested_tuple function on each tuple element.
- Append the maximum value of each pair of tuple elements to the list initialized in step 3.
- Convert the list to a tuple using the tuple() method and return the tuple.
Python3
# function to perform task
def tup_max(tup1, tup2):
if isinstance(tup1, tuple) and isinstance(tup2, tuple):
res = tuple(tup_max(t1, t2) for t1, t2 in zip(tup1, tup2))
return res
return max(tup1, tup2)
# initialize tuples
test_tup1 = ((1, 3), (4, 5), (2, 9), (1, 10))
test_tup2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Maximizing Nested Tuples using recursion
res = tup_max(test_tup1, test_tup2)
# printing result
print("The resultant tuple after maximization : " + str(res))
OutputThe original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after maximization : ((6, 7), (4, 9), (2, 9), (7, 10))
Time complexity: The time complexity of this method is O(n^2), where n is the number of elements in the tuples. This is because it involves recursive function calls for each element in the tuples.
Auxiliary space: The auxiliary space used by this method is O(n^2) as well, since it creates new tuples at each recursive call.
Similar Reads
Python - Flatten Nested Tuples
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform flattening of tuples, which can be nested and undesired. This can have application across many domains such as Data Science and web development. Let's discuss certain way in which this task can be performe
7 min read
Addition in Nested Tuples - Python
Sometimes, while working with records, we can have a problem in which we require to perform index wise addition of tuple elements. This can get complicated with tuple elements to be tuple and inner elements again be tuple. Let's discuss certain ways in which this problem can be solved. Method #1: U
6 min read
Python - Extend consecutive tuples
Given list of tuples, join consecutive tuples. Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)] Output : [(3, 5, 6, 7, 3, 2, 4, 3), (3, 2, 4, 3, 9, 4), (9, 4, 2, 3, 2)] Explanation : Elements joined with their consecutive tuples. Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3)] Ou
3 min read
Python | Compare tuples
Sometimes, while working with records, we can have a problem in which we need to check if each element of one tuple is greater/smaller than it's corresponding index in other tuple. This can have many possible applications. Let's discuss certain ways in which this task can be performed. Method #1 : U
4 min read
Unpacking Nested Tuples-Python
The task of unpacking nested tuples in Python involves iterating through a list of tuples, extracting values from both the outer and inner tuples and restructuring them into a flattened format. For example, a = [(4, (5, 'Gfg')), (7, (8, 6))] becomes [(4, 5, 'Gfg'), (7, 8, 6)].Using list comprehensio
3 min read
Python Access Tuple Item
In Python, a tuple is a collection of ordered, immutable elements. Accessing the items in a tuple is easy and in this article, we'll explore how to access tuple elements in python using different methods.Access Tuple Items by IndexJust like lists, tuples are indexed in Python. The indexing starts fr
2 min read
Python - Pairwise Addition in Tuples
Sometimes, while working with data, we can have a problem in which we need to find cumulative result. This can be of any type, product or summation. Here we are gonna discuss about adjacent element addition. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + ge
4 min read
Python | Addition of tuples
Sometimes, while working with records, we might have a common problem of adding contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let's discuss certain ways in which this task can be performed. Metho
5 min read
Find Largest Item in a Tuple - Python
We need to find the largest number or the top N largest numbers from a tuple.For Example:Input: (10,20,23,5,2,90) #tupleOutput: 90Explanation: 90 is the largest element from tupleThere are several efficient ways to achieve this:Using max()max() in Python is the most efficient way to find the largest
3 min read
Python | Nested Tuples Subtraction
Sometimes, while working with records, we can have a problem in which we require to perform index-wise subtraction of tuple elements. This can get complicated with tuple elements being tuples and inner elements again being tuple. Letâs discuss certain ways in which this problem can be solved. Metho
7 min read