Reputation: 20916
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)]
Upvotes: 2
Views: 361
Reputation: 20136
lambda student: student[2]
is just a function and student
is its argument, it could also be:
sorted(student_tuples, key=lambda x: x[2])
so what is going on here is that you are giving sorted a list and a function that operates on every item and it will sort the list based on the result of that function
From sorted
documentation:
cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.
So you could use any function for sorting, and lambda function is just a type of anonymous function (a function without a name)
Upvotes: 0
Reputation: 77167
The entire list is not recognized by the lambda. Only the individual members of the list to be sorted are passed.
>>> def age(student):
... print "member: %s" % repr(student)
... return student[2]
...
>>> sorted(student_tuples, key=age)
member: ('john', 'A', 15)
member: ('jane', 'B', 12)
member: ('dave', 'B', 10)
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Upvotes: 4
Reputation: 65649
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.
Upvotes: 0
Reputation: 3111
The sorted
function's key
argument takes a function of a single argument (the lambda in this case). As sorted iterates through the input list, it uses the key
function to obtain the sorting value, in this case, the third item in the input tuple.
The student
name in your lambda is just a name, you could use lambda x: x[2]
with the same effect.
Upvotes: 0
Reputation: 56418
For sorted
the key
argument is a one-argument function that is expected to take an element of the sequence being sorted and return what is considered to be the "key" for that element. In your case, it's the third item in each of the tuples. So instead of sorting the tuples the way they normally would (based on values left-to-right), you are saying you have a "key" for each tuple that better represents how they should be sorted and it is the third value in each tuple.
So for your example, you have:
lambda student: student[2]
This is basically equivalent to a function that looks like this:
def get_my_key(item):
return item[2]
So when sorting your sequence, it's calling this get_my_key
function on each item to retrieve the actual key.
Upvotes: 2
Reputation: 383
I think is not recognized. student is just the name you put to the object, could be x
, like in
>>>sorted(student_tuples, key=x: x[2])
If your objects are not lists, or doesn't have a '2' element, you will get a error.
Upvotes: 0