python sort list

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

Here is what the above code is Doing:
1. We have a list of tuples. Each tuple contains a student’s name, class and age.
2. We use sorted() to sort the list by age.
3. We use the key parameter to specify a function that will be called on each list element before making comparisons.
4. The function takes a student tuple as input and returns the student’s age.
5. This means that when sorted() compares two tuples, it will use the age as a comparison key.
6. The sorted() function will sort the tuples by age in ascending order.