0
Input : [[0, 2], [1, 4], [2, 6]]

Description : I need to print two lists with greater value by comparing the element in the 2nd place.

Expected Output: [[1, 4], [2, 6]]
yatu
  • 86,083
  • 12
  • 84
  • 139
Santhosh
  • 209
  • 2
  • 16

1 Answers1

1

You can use sorted and specify in the key argument that you want to sort each sublist by the second element using operator.itemgetter. Then slice the returned list to select the two last sublists:

l = [[0, 2], [1, 4], [2, 6]]

from operator import itemgetter
sorted(l, key=itemgetter(1))[-2:]

Output

[[1, 4], [2, 6]]
yatu
  • 86,083
  • 12
  • 84
  • 139