- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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)) print ("The name that has the maximum value is : ") print(my_result)
The list is : [('Will', 23), ('Jane', 21), ('El', 24), ('Min', 101)] The name that has the maximum value is : Min
- 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.
- 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
- Flatten tuple of List to tuple in Python
- 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
- Get tuple element data types in Python
- Why python returns tuple in list instead of list in list?
- Get first element with maximum value in list of tuples in Python
- Find Maximum difference between tuple pairs in Python
- List vs tuple vs dictionary in Python
- Python - Join tuple elements in a list
- Difference Between List and Tuple in Python