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
  • 80,714
  • 11
  • 64
  • 111
Santhosh
  • 189
  • 1
  • 15
  • Did it help @Santhosh ? remember to upvote/accept if so. See [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – yatu Mar 04 '19 at 11:15
  • Use duplicate to sort - slice as many as you want (2) from sorted result. – Patrick Artner Mar 04 '19 at 13:59

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
  • 80,714
  • 11
  • 64
  • 111