
- 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
Get first element with maximum value in list of tuples in Python
We have a list of tuple. We are required to find out that tuple which was maximum value in it. But in case more than one tuple has same value we need the first tuple which has maximum value.
With itemgetter and max
With itemgetter(1) we get all the value from index position 1 and then apply a max function to get the item with max value. But in case more than one result is returned, we apply index zero to get the first tuple with maximum element in it.
Example
from operator import itemgetter # initializing list listA = [('Mon', 3), ('Tue', 20), ('Wed', 9)] # Given list print("Given list : \n" ,listA) # using max() and itemgetter() res = max(listA, key=itemgetter(1))[0] # printing result print("Day with maximum score is : \n",res)
Output
Running the above code gives us the following result −
Given list : [('Mon', 3), ('Tue', 20), ('Wed', 9)] Day with maximum score is : Tue
With max and lambda
We use the lambda function to get the elements at index position 1 then apply the max function. Then we apply the index position 0 to get the first among multiple matches to get the final result.
Example
# initializing list listA = [('Mon', 3), ('Tue', 20), ('Wed', 9)] # Given list print("Given list : \n" ,listA) # using max() and lambda res = max(listA, key = lambda i : i[1])[0] # printing result print("Day with maximum score is : \n",res)
Output
Running the above code gives us the following result −
Given list : [('Mon', 3), ('Tue', 20), ('Wed', 9)] Day with maximum score is : Tue
With sorted
In this approach we use the sorted function with reversed equal to true condition when applying the lambda function.
Example
# initializing list listA = [('Mon', 3), ('Tue', 20), ('Wed', 9)] # Given list print("Given list : \n" ,listA) # using sorted() and lambda res = sorted(listA, key = lambda i: i[1], reverse = True)[0][0] # printing result print("Day with maximum score is : \n",res)
Output
Running the above code gives us the following result −
Given list : [('Mon', 3), ('Tue', 20), ('Wed', 9)] Day with maximum score is : Tue
- Related Articles
- Python Group tuples in list with same first value
- Remove tuples having duplicate first value from given list of tuples in Python
- Python - Get sum of tuples having same first value
- Selective value selection in list of tuples in Python
- Accessing nth element from Python tuples in list
- Rear element extraction from list of tuples records in Python
- Find the tuples containing the given element from a list of tuples in Python
- Combining tuples in list of tuples in Python
- Get key with maximum value in Dictionary in Python
- Filter Tuples by Kth element from List in Python
- Filter tuples according to list element presence in Python
- Find the Maximum of Similar Indices in two list of Tuples in Python
- Python program to find Tuples with positive elements in List of tuples
- Python - Restrict Tuples by frequency of first element’s value
- Python – Maximum of K element in other list
