Home > Net >  How to find max in list of lists?
How to find max in list of lists?

Time:08-08

Let's say I have the following list:

[[2016, 'May', 16.9],
 [2016, 'April', 17.5],
 [2016, 'March', 17.8],
 [2016, 'February', 18.6],
 [2016, 'January', 18.8],
 [2015, 'December', 19.1],
 [2015, 'November', 19.2],
 [2015, 'October', 20.0],
 [2015, 'September', 20.6],
 [2015, 'August', 21.2],
 [2015, 'July', 21.6],
 [2015, 'June', 21.3],
 [2015, 'May', 21.5],
 [2015, 'April', 21.6],
 [2015, 'March', 22.1]]

I would like to get back the list as in [2015, 'March', 22.1], for where the last element is the highest out of the entire list.

What would be a good way?

CodePudding user response:

Try using builtin max function with proper lambda.

max(data, key=lambda x: x[2])

CodePudding user response:

sorted(llist, key= lambda x: x[2])[-1]

where llist is your list.

  • Related