- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Get maximum of Nth column from tuple list in Python
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
- A list of tuples is defined, and displayed on the console.
- The value of 'N' is initialized.
- The list of tuples is iterated through, using list comprehension, and the 'amx' method is used to get the maximum value from the list of tuples.
- This operation is assigned a variable.
- This variable is the output that is displayed on the console.
- Related Articles
- Consecutive Nth column Difference in Tuple List using Python
- Get Nth Column of Matrix in Python
- Python – Extract Kth element of every Nth tuple in List
- Maximum element in tuple list in Python
- Kth Column Product in Tuple List in Python
- Remove Nth Node From End of List in Python
- Maximum value in record list as tuple attribute in Python
- Accessing nth element from Python tuples in list
- Extract digits from Tuple list Python
- Flatten tuple of List to tuple in Python
- Get Nth weekday of the month from a column with DATE records in MySQL
- Find minimum k records from tuple list in Python
- Create a tuple from string and list in Python
- Get maximum date from a list of varchar dates in MySQL
- Python - Column deletion from list of lists