max() of a list of tuples

Max M maxm at mxm.dk
Tue Jan 21 12:25:58 CET 2003


Mario Wehbrink wrote:

 > i have a list of tuples that look like:
 > [(1,3,5), (8,16,2), (2,56,4)]
 >
 > what i am interested, in is the tuple with the greatest value in pos 3.
 > So in this case it would be (1,3,5). Is there a way to tell
 > max(mylistoftuples) to only look at the last position of the tuples?


l = [(1,3,5), (8,16,2), (2,56,4)]
print max([(i[-1],i) for i in l])[1]

 >>> (1,3,5)

-- 

hilsen/regards Max M

http://www.futureport.dk/
Fremtiden, videnskab, skeptiscisme og transhumanisme





More information about the Python-list mailing list