While using some built-in functions like sorted, sum...
I noticed the usage of key=lambda
What is lambda? How does it work?
What other functions use key=lambda?
Are there any other key values like, key=?
While using some built-in functions like sorted, sum...
I noticed the usage of key=lambda
What is lambda? How does it work?
What other functions use key=lambda?
Are there any other key values like, key=?
A
lambda
is an anonymous function:It is often used in functions such as
sorted()
that take a callable as a parameter (often thekey
keyword parameter). You could provide an existing function instead of alambda
there too, as long as it is a callable object.Take the
sorted()
function as an example. It'll return the given iterable in sorted order:but that sorts uppercased words before words that are lowercased. Using the
key
keyword you can change each entry so it'll be sorted differently. We could lowercase all the words before sorting, for example:We had to create a separate function for that, we could not inline the
def lowercased()
line into thesorted()
expression:A
lambda
on the other hand, can be specified directly, inline in thesorted()
expression:Lambdas are limited to one expression only, the result of which is the return value.
There are loads of places in the Python library, including built-in functions, that take a callable as keyword or positional argument. There are too many to name here, and they often play a different role.
Lambda can be any function. So if you had a function
You could sort a list of Person (each of which having an age attribute) like this:
This way, the list would be sorted by age in ascending order.
The parameter is called lambda because python has a nifty lambda keywords for defining such functions on the fly. Instead of defining a function compare_person and passing that to sorted, you can also write:
which does the same thing.
Actually, above codes can be:
According to https://docs.python.org/2/library/functions.html?highlight=sorted#sorted, key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).
In Python, lambda is a keyword used to define anonymous functions(functions with no name) and that's why they are known as lambda functions.
Basically it is used for defining anonymous functions that can/can't take argument(s) and returns value of data/expression. Let's see an example.
Now let me give an answer of your 2nd question. The 1st answer is also great. This is my own way to explain with another example.
Suppose we have a list of items(integers and strings with numeric contents) as follows,
and I want to sort it using sorted() function, lets see what happens.
It didn't give me what I expected as I wanted like below,
It means we need some strategy(so that sorted could treat our string items as an ints) to achieve this. This is why the key keyword argument is used. Please look at the below one.
Lets use lambda function as a value of key
You can define your own function(callable) and provide it as value of key.
Dear programers, I have written the below code for you, just try to understand it and comment your explanation. I would be glad to see your explanation(it's simple).
I hope it would be useful.