Ethan Brouwer
Ethan Brouwer

Reputation: 1005

Python - 2d list max based on certain part of inner list

So I have a 2-dimensional list (as in a list of lists) and I'm trying to find the largets inner list based on their third element.

This is what I have so far, but it only returns j[2], not the entire list j where j[2] is the biggest.

big = max([int(j[2]) for j in cur2])

What I want it to do is return the entire max list out of each of the lists inside of cur2 dependent on the size of int(j[2])

Please don't refer to this question because I don't really understand how to apply that answer to my situation.

Upvotes: 0

Views: 532

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250941

Try this:

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

Example:

>>> cur2=[range(4),range(4,8),range(2,6)]
>>> cur2
[[0, 1, 2, 3], [4, 5, 6, 7], [2, 3, 4, 5]]
>>> max( cur2, key=lambda x:int(x[2]))
[4, 5, 6, 7]

Upvotes: 2

Related Questions