Python Lambda Function

Python allows you to create anonymous function i.e function having no names using a facility called lambda function.

lambda functions are small functions usually not more than a line. It can have any number of arguments just like a normal function. The body of lambda functions is very small and consists of only one expression. The result of the expression is the value when the lambda is applied to an argument. Also there is no need for any return statement in lambda function.

Let’s take an example:

Consider a function multiply()

This function is too small, so let’s convert it into a lambda function.

To create a lambda function first write keyword lambda followed by one of more arguments separated by comma, followed by colon sign ( : ), followed by a single line expression.

Expected Output:

Here we are using two arguments x  and y , expression after colon is the body of the lambda function. As you can see lambda function has no name and is called through the variable it is assigned to.

You don’t need to assign lambda function to a variable.

Expected Output:

Note that lambda function can’t contain more than one expression.

Leave a Reply

1 Comment on "Python Lambda Function"

Notify of

Sort by:   newest | oldest | most voted
2 months 11 days ago

good article.Thanks for this simple description on python lambda function.