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)]
Remember python is dynamically typed. The lambda doesn’t know what
studentis before it unpacks it and tries to use it. Its a name for something in python.studentcould be an integer, a list, an object, or whatever. There’d be no validation done until the lambakeywas executed andpythonrealized thatstudentwas not indexable.