finding maximum value in python list of tuples -


this question has answer here:

i have 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 run this, this:

>>> print(max(list)) ('2015-06-19', 3453455, 5, 'scheduled') 

obviously, max(list) determined max based on first value in list of tuples. i'm wondering if default behavior of max(list) in regards list of tuples; check first item in tuple?

and, if wanted return tuple maximum based on second item/column. how that?

you can specify field use comparison using lambdas

max(list,key=lambda x:x[1]) 

or alternatively, using itemgetter

from operator import itemgetter max(list,key=itemgetter(1)) 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -