Ljupcho Naumov
Ljupcho Naumov

Reputation: 465

How to find max in list of lists?

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?

Upvotes: 0

Views: 183

Answers (2)

Nuri Taş
Nuri Taş

Reputation: 3845

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

where llist is your list.

Upvotes: 0

Piotr Grzybowski
Piotr Grzybowski

Reputation: 594

Try using builtin max function with proper lambda.

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

Upvotes: 3

Related Questions