2

I have data in the following form

data = [["one", 1], ["three", 3], ["two", 2]]

From this list, I want to find the sub-list where the second item is maximum.
I can do this like so:

max_i = max([i[1] for i in data])
[i for i in filter(lambda i: i[1] == max_i, data)][0]

But this doesn't seem very elegant syntax. Is there a better way with clearer syntax?

Max888
  • 3,089
  • 24
  • 55

1 Answers1

4

max function also takes a key argument which specifies a one-argument ordering function.

>>> data = [["one", 1], ["three", 3], ["two", 2]]
>>> max(data, key=lambda x: x[1])
['three', 3]
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46