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?
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?
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]]
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]]