I have a list of tuples (list):
('2015-06-19', 3453455, 5, 'Scheduled')
('2015-05-19', 6786788, 6, 'Overdue')
('2015-04-19', 2342344, 2, 'Not Received')
('2015-03-19', 9438549, 0, 'Not Received')
('2015-02-19', 6348759, 7, 'Not Received')
When I run this, I get this:
>>> print(max(list))
('2015-06-19', 3453455, 5, 'Scheduled')
Obviously, max(list) determined the max based on the first value in the list of tuples. I'm just wondering if this is the default behavior of max(list) in regards to a list of tuples; to check only the first item in the tuple?
And, what if I wanted to return the tuple with the maximum based on the second item/column. How would I do that?
You can specify which field to use for comparison using lambdas
max(list,key=lambda x:x[1])
or alternatively, using itemgetter
from operator import itemgetter
max(list,key=itemgetter(1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With