[Solved] Get max value in a two value list Python [duplicate]


Use the max built-in function with key kwarg. Refer to the docs of max for more details.

It will return the sublist that has the max number, from which we take the first element (the “name”).

li = [['ABC', 1.4976557902646848], ['LMN', 1.946130694688788], ['QRS', 3.0039607941124085]]

print(max(li, key=lambda x: x[1])[0])
# QRS

You can use itemgetter instead of defining the lambda:

from operator import itemgetter

li = [['ABC', 1.4976557902646848], ['LMN', 1.946130694688788], ['QRS', 3.0039607941124085]]

print(max(li, key=itemgetter(1))[0])
# QRS

solved Get max value in a two value list Python [duplicate]