Can someone elaborate on how the below statements work?
My question is related to the lambda function? The lambda statement takes the student as the input and returns (student[2]) second element in the list. I understand we have the student_tuples as the list but how does the "Student" list is recognized by the Python lambda function..
>>> 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)]
but how does the "Student" list is recognized by the Python lambda function..
Remember python is dynamically typed. The lambda doesn't know what student
is before it unpacks it and tries to use it. Its a name for something in python. student
could be an integer, a list, an object, or whatever. There'd be no validation done until the lamba key
was executed and python
realized that student
was not indexable.