Python | Get first element with maximum value in list of tuples
In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let’s discuss certain ways in which this can be achieved.
Method #1 : Using max() + operator.itemgetter()
We can get the maximum of corresponding tuple index from a list using the key itemgetter index provided and then mention the index information required using index specification at the end.
# Python3 code to demonstrate # to get tuple info. of maximum value tuple # using max() + itemgetter() from operator import itemgetter # initializing list test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)] # printing original list print ("Original list : " + str(test_list)) # using max() + itemgetter() # to get tuple info. of maximum value tuple res = max(test_list, key = itemgetter(1))[0] # printing result print ("The name with maximum score is : " + res) |
Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with maximum score is : Manjeet
Method #2 : Using max() + lambda
This method is almost similar to the method discussed above, just the difference is the specification and processing of target tuple index for maximum is done by lambda function. This improved readability of code.
# Python3 code to demonstrate # to get tuple info. of maximum value tuple # using max() + lambda # initializing list test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)] # printing original list print ("Original list : " + str(test_list)) # using max() + lambda # to get tuple info. of maximum value tuple res = max(test_list, key = lambda i : i[1])[0] # printing result print ("The name with maximum score is : " + res) |
Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with maximum score is : Manjeet
Method #3 : Using sorted() + lambda
The task performed by the max() in the above two methods can be done using reverse sorting and printing the first element. lambda function performs the task similar to above said methods.
# Python3 code to demonstrate # to get tuple info. of maximum value tuple # using sorted() + lambda # initializing list test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)] # printing original list print ("Original list : " + str(test_list)) # using sorted() + lambda # to get tuple info. of maximum value tuple res = sorted(test_list, key = lambda i: i[1], reverse = True)[0][0] # printing result print ("The name with maximum score is : " + res) |
Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with maximum score is : Manjeet
Recommended Posts:
- Python | Find the tuples containing the given element from a list of tuples
- Python | Accessing nth element from tuples in list
- Python | Preceding element tuples in list
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Combining tuples in list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of strings
- Python | Maximum and minimum element's position in a list
- Python | Convert list of tuples to list of list
- Python | Update a list of tuples using another list
- Python | Convert list of tuples into list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



