0

I have this list of lists:

data = [['john','1','2', 2.45434], ['jack','1.3','2.2', 4.45434],
        ['josh','2','3', 3.45434]]

I want to sort it by the very last decimal number in each inner list. How can I do this?

martineau
  • 119,623
  • 25
  • 170
  • 301
jojo
  • 73
  • 2
  • 10
  • In your case, the index is `-1` instead of `1`, but the concept is the same as the duplicate. – zondo Aug 25 '16 at 00:31

2 Answers2

1

You can specify the key in the sorted function as the last element of the sub lists:

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

# [['john', '1', '2', 2.45434],
#  ['josh', '2', '3', 3.45434],
#  ['jack', '1.3', '2.2', 4.45434]]
Psidom
  • 209,562
  • 33
  • 339
  • 356
0

You may sort the existing "data" list by .sort() function of list using lambda function as:

data = [['john','1','2', 2.45434],['jack','1.3','2.2', 4.45434], ['josh','2','3', 3.45434]]
data.sort(key=lambda x: x[-1])
# data = [['john', '1', '2', 2.45434], ['josh', '2', '3', 3.45434], ['jack', '1.3', '2.2', 4.45434]]
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126