>>> student_tuples = [
... ('john', 'A', 15),
... ('jane', 'B', 12),
... ('dave', 'B', 10),
... ]
>>> sorted(student_tuples, key=lambda student: student[2])
# sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
# sort list by key
st1 = [ [ 1, 2], [3, 4], [4, 2], [ -1, 3], [ 4, 5], [2, 3]]
st1.sort()
print(st1) # sort by 1st element [ [ -1, 3 ], [ 1, 2 ], [ 2, 3 ] ...
st1.sort(reverse=True)
print(st1) # sort decending [ [ 4, 5 ], [ 3, 4], [ 2, 3 ] ...
st1.sort(key=lambda x: x[1]) # sort by 2nd element accending
print(st1) # [ [ 1, 2 ], [ 4, 2 ], [ -1, 3 ] ...
st1.sort(key=lambda x: x[0] + x[1]) # sort by sum of 1st and 2nd element accending
print(st1) # [ [ -1, 3 ], [ 1, 2 ], [ 2, 3 ] ...
# lambda undefined function
def sort_func(x) : # classic way
return x[0] + x[1]
st1.sort(key=sort_func)
st1.sort(key=lambda x: x[0] + x[1]) # defined function lambda at place
# sort list by key
st1 = [ [ 1, 2], [3, 4], [4, 2], [ -1, 3], [ 4, 5], [2, 3]]
st1.sort()
print(st1) # sort by 1st element [ [ -1, 3 ], [ 1, 2 ], [ 2, 3 ] ...
st1.sort(reverse=True)
print(st1) # sort decending [ [ 4, 5 ], [ 3, 4], [ 2, 3 ] ...
st1.sort(key=lambda x: x[1]) # sort by 2nd element accending
print(st1) # [ [ 1, 2 ], [ 4, 2 ], [ -1, 3 ] ...
st1.sort(key=lambda x: x[0] + x[1]) # sort by sum of 1st and 2nd element accending
print(st1) # [ [ -1, 3 ], [ 1, 2 ], [ 2, 3 ] ...
# lambda undefined function
def sort_func(x) : # classic way
return x[0] + x[1]
st1.sort(key=sort_func)
st1.sort(key=lambda x: x[0] + x[1]) # defined function lambda at place