When it is required to get the maximum of 'N'th column from a list of tuples, it can be done using list comprehension and 'max' method.
The list comprehension is a shorthand to iterate through the list and perform operations on it. The 'max' method returns the maximum of values among an iterable.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
A list of tuple basically contains tuples enclosed in a list.
Below is a demonstration of the same −
my_list = [( 67, 78, 39), (34, 23, 52), (99, 69, 78), (2, 11, 0)] print ("The list is : " ) print(my_list) N = 2 print("The value of 'N' has been initialized") my_result = max([sub[N] for sub in my_list]) print("The maximum of Nth column in the list of tuples is : " ) print(my_result)
The list is : [(67, 78, 39), (34, 23, 52), (99, 69, 78), (2, 11, 0)] The value of 'N' has been initialized The maximum of Nth column in the list of tuples is : 78