
- 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
Maximum element in tuple list in Python
When it is required to find the maximum element in a tuple list (i.e list of tuples), the 'max' method and the 'operator.itemgetter' method can be used.
The itemgetter fetches a specific item from its operand.
The 'max' method gives the maximum value present in an iterable that is passed as argument to it.
Below is a demonstration of the same −
Example
from operator import itemgetter my_list = [('Will', 23), ('Jane', 21), ('El', 24), ('Min', 101)] print ("The list is : ") print(my_list) my_result = max(my_list, key = itemgetter(1))[0] print ("The name that has the maximum value is : ") print(my_result)
Output
The list is : [('Will', 23), ('Jane', 21), ('El', 24), ('Min', 101)] The name that has the maximum value is : Min
Explanation
- The required libraries are imported.
- The list of tuples is defined, and is displayed on the console.
- The 'max' method is used to go through the list, and specify the key as the first element of every tuple inside the list.
- This result is assigned to a value.
- It is displayed as output on the console.
- Related Articles
- Update each element in tuple list in Python
- Maximum value in record list as tuple attribute in Python
- Get maximum of Nth column from tuple list in Python
- Python – Maximum of K element in other list
- Python – Check if any list element is present in Tuple
- Python – Extract Kth element of every Nth tuple in List
- Python – Test if tuple list has a single element
- Flatten tuple of List to tuple in Python
- Get first element with maximum value in list of tuples in Python
- Python – Cross Pairing in Tuple List
- Why python returns tuple in list instead of list in list?
- Get tuple element data types in Python
- Find Maximum difference between tuple pairs in Python
- Python - Join tuple elements in a list
- List vs tuple vs dictionary in Python

Advertisements