Python - Records Maxima in List of Tuples
Last Updated :
09 Apr, 2023
Sometimes, while working with records, we can have a problem in which we need to the maximum all the columns of a container of lists that are tuples. This kind of application is common in the web development domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using max() + list comprehension + zip()
This task can be performed using a combination of above functions. In this, we cumulate the like index elements, i.e columns using zip(), and then iterate through them using list comprehension and perform maximum using max().
Python3
# Python3 code to demonstrate working of
# Records Maxima in List of Tuples
# using list comprehension + max() + zip()
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
# printing original list
print("The original list : " + str(test_list))
# Records Maxima in List of Tuples
# using list comprehension + max() + zip()
res = [max(ele) for ele in zip(*test_list)]
# printing result
print("The Cumulative column maximum is : " + str(res))
Output:
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column maximum is : [6, 7, 8]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(m), where m is the length of the longest tuple in the list.
Method #2 : Using zip() + map() + max()
This method is similar to the above method. In this, the task performed by list comprehension is performed by map(), which extends the maximum of columns to zipped elements.
Python3
# Python3 code to demonstrate working of
# Records Maxima in List of Tuples
# using zip() + map() + max()
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
# printing original list
print("The original list : " + str(test_list))
# Records Maxima in List of Tuples
# using zip() + map() + max()
res = list(map(max, zip(*test_list)))
# printing result
print("The Cumulative column maximum is : " + str(res))
Output:
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column maximum is : [6, 7, 8]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(m), where m is the length of the longest tuple in the list.
Method#3: Using Recursive method.
Python3
def find_maxima_recursive(lst, index, result):
if not lst:
return result
else:
result[index] = max(result[index], lst[0][index])
return find_maxima_recursive(lst[1:], index, result)
def find_maxima(lst):
if not lst:
return []
result = [0] * len(lst[0])
for i in range(len(lst[0])):
find_maxima_recursive(lst, i, result)
return result
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
# printing original list
print("The original list : " + str(test_list))
res=find_maxima(test_list)
# printing result
print("The Cumulative column maximum is : " + str(res))
#this code contributed by tvsk
OutputThe original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column maximum is : [6, 7, 8]
Time Complexity: O(n*m)
Auxiliary Space: O(n)
Method#4: using enumerate() + max() + zip()
Algorithm:
- Initialize an empty list "res".
- Loop through each tuple in the list "test_list".
- If it is the first tuple, add it to the "res" list as is.
- If it is not the first tuple, compare each element in the current tuple with its corresponding element in the "res" list and keep the maximum value.
- Update the "res" list with the maximum values.
- Return the "res" list.
Python3
# Python3 code to demonstrate working of
# Records Maxima in List of Tuples
# using enumerate() + max() + zip()
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
# printing original list
print("The original list : " + str(test_list))
# Records Maxima in List of Tuples
# using enumerate() + max() + zip()
res = []
for i, tup in enumerate(test_list):
if i == 0:
res = list(tup)
else:
res = [max(x, y) for x, y in zip(res, tup)]
# printing result
print("The Cumulative column maximum is : " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column maximum is : [6, 7, 8]
Time complexity: O(n*m), where n is the number of tuples in the list and m is the number of elements in each tuple. The loop runs n times, and the max() function runs m times for each iteration of the loop.
Auxiliary Space: O(m), where m is the number of elements in each tuple. The "res" list stores the maximum values for each element, so its length is equal to the number of elements in each tuple..
Method#5: Using numpy:
Algorithm:
1.Import the required library numpy.
2.Initialize the list of tuples.
3.Use the numpy library's max function and pass the list of tuples as a parameter, and set the axis as 0. This will find the maximum value of each 4.element in each tuple for all the tuples.
5.Convert the numpy array to a list and print the result.
Python3
import numpy as np
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
# printing original list
print("The original list : " + str(test_list))
# finding maximum of each column using numpy's max() function and setting axis=0 to get column-wise maximums
max_list = np.max(test_list, axis=0)
# printing the resulting list as a Python list using tolist() method
print("The Cumulative column maximum is:", max_list.tolist())
#This code is contributed by Jyothi pinjala.
Output:
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column maximum is: [6, 7, 8]
Time Complexity: O(N*M), where N is the number of tuples and M is the number of elements in each tuple.
Space Complexity: O(M), where M is the number of elements in each tuple.
Similar Reads
Python | Maximize Record list Sometimes, while working with Python records, we can have a problem in which we need to perform cross maximization of list of tuples. This kind of application is popular in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension +
6 min read
Python - Maximum of Similar Keys in Tuples Sometimes, while working with Python tuples, we can have a problem in which we need to perform maximum of all values of the equal keys in Tuple list. This kind of application in useful in many domains such as web development and day-day programming. Let's discuss certain ways in which this task can
4 min read
Min and Max value in list of tuples-Python The task of finding the minimum and maximum values in a list of tuples in Python involves identifying the smallest and largest elements from each position (column) within the tuples. For example, given [(2, 3), (4, 7), (8, 11), (3, 6)], the first elements (2, 4, 8, 3) have a minimum of 2 and a maxim
3 min read
Python - Maximum value in record list as tuple attribute Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the max of a list as a tuple attribute. Letâs discuss certai
8 min read
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read