Possible Duplicate:
Sorting or Finding Max Value by the second element in a nested list. Python
I have a list with ~10^6 tuples in it like this:
[(101, 153), (255, 827), (361, 961), ...]
^ ^
X Y
I want to find the maximum value of the Ys in this list, but also want to know the X that it is bound to.
How do I do this?
In addition to max, you can also sort:
>>> lis
[(101, 153), (255, 827), (361, 961)]
>>> sorted(lis,key=lambda x: x[1], reverse=True)[0]
(361, 961)
You could loop through the list and keep the tuple in a variable and then you can see both values from the same variable...
num=(0, 0)
for item in tuplelist:
if item[1]>num[1]:
num=item #num has the whole tuple with the highest y value and its x value
Source: Stackoverflow.com