Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get first element with maximum value in list of tuples in Python
When working with a list of tuples, you may need to find the first element of the tuple that has the maximum value in a specific position. This is useful when you have data like scores, dates, or measurements stored as tuples and want to identify the tuple with the highest value.
Using itemgetter() and max()
The itemgetter() function from the operator module extracts values from a specific index position. Combined with max(), it finds the tuple with the maximum value ?
from operator import itemgetter
# initializing list
data = [('Mon', 3), ('Tue', 20), ('Wed', 9)]
# Given list
print("Given list:")
print(data)
# using max() and itemgetter()
result = max(data, key=itemgetter(1))[0]
# printing result
print("Day with maximum score is:")
print(result)
Given list:
[('Mon', 3), ('Tue', 20), ('Wed', 9)]
Day with maximum score is:
Tue
Using max() and lambda
A lambda function provides a more concise way to specify the key for comparison. It extracts the element at index position 1, then max() returns the entire tuple with the highest value ?
# initializing list
data = [('Mon', 3), ('Tue', 20), ('Wed', 9)]
# Given list
print("Given list:")
print(data)
# using max() and lambda
result = max(data, key=lambda i: i[1])[0]
# printing result
print("Day with maximum score is:")
print(result)
Given list:
[('Mon', 3), ('Tue', 20), ('Wed', 9)]
Day with maximum score is:
Tue
Using sorted() with reverse=True
This approach sorts the entire list in descending order based on the second element, then takes the first tuple from the sorted result ?
# initializing list
data = [('Mon', 3), ('Tue', 20), ('Wed', 9)]
# Given list
print("Given list:")
print(data)
# using sorted() and lambda
result = sorted(data, key=lambda i: i[1], reverse=True)[0][0]
# printing result
print("Day with maximum score is:")
print(result)
Given list:
[('Mon', 3), ('Tue', 20), ('Wed', 9)]
Day with maximum score is:
Tue
Handling Ties
When multiple tuples have the same maximum value, all methods return the first occurrence ?
# List with duplicate maximum values
data = [('Mon', 20), ('Tue', 20), ('Wed', 9)]
print("Given list:")
print(data)
# All methods return the first tuple with max value
result1 = max(data, key=itemgetter(1))[0]
result2 = max(data, key=lambda i: i[1])[0]
result3 = sorted(data, key=lambda i: i[1], reverse=True)[0][0]
print(f"itemgetter result: {result1}")
print(f"lambda result: {result2}")
print(f"sorted result: {result3}")
Given list:
[('Mon', 20), ('Tue', 20), ('Wed', 9)]
itemgetter result: Mon
lambda result: Mon
sorted result: Mon
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
max() + itemgetter() |
O(n) | Clean, readable code |
max() + lambda |
O(n) | Simple, no imports needed |
sorted() + reverse |
O(n log n) | When you need sorted data |
Conclusion
Use max() with either itemgetter() or lambda for efficient O(n) solutions. The sorted() approach is less efficient but useful when you need the entire sorted list. All methods correctly handle ties by returning the first occurrence.
