1>>> student_tuples = [
2... ('john', 'A', 15),
3... ('jane', 'B', 12),
4... ('dave', 'B', 10),
5... ]
6>>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
7[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
8
1#return new list
2>>> student_tuples = [
3... ('john', 'A', 15),
4... ('jane', 'B', 12),
5... ('dave', 'B', 10),
6... ]
7>>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
8[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
9
10#sort in place
11data = [("Apples", 5, "20"), ("Pears", 1, "5"), ("Oranges", 6, "10")]
12
13data.sort(key=lambda x:x[0])
14print(data)
15OUTPUT
16[('Apples', 5, '20'), ('Oranges', 6, '10'), ('Pears', 1, '5')]
17
18from kite.com