It is unclear to me how the following works:
In [1]: student_tuples = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
In [2]: sorted(student_tuples, key=lambda student: student[2])
Out [2]: [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)] # sort by age
but,
In [3]: st = lambda student: student[2]
In [4]: st(student_tuples)
Out [4]: ('dave', 'B', 10)
Why does the [2]
in the former sample refer to the index for the individual tuples, when in a lambda function it returns the 2nd tuple in the list?