DEV Community

Divyanshu Shekhar
Divyanshu Shekhar

Posted on

Python Lambda Function

Python Lambda Function

Normally while defining a function in python we use the def keyword but when an anonymous function is declared we use the lambda keyword. These functions have no name.

And thus called the anonymous function.

Python Lambda Function Syntax

lambda arguments: expression
Enter fullscreen mode Exit fullscreen mode

The thing to keep in mind when using the lambda function is, there can be zero or multiple arguments separated by commas but only a single expression. Argument and expression in the lambda function are separated by a colon (:).

Expression in lambda functions is evaluated and returned. The whole lambda function returns a function object.

Learn Python list comprehension, an easy way to create a list in a single line.

The function object returned by the lambda function is stored in a variable. As the variable contains a function (Functions in python are also first-class citizens) it can be executed with the number of arguments passed in the lambda function.

Read the whole post Python Lambda Function from the original Post.

Top comments (0)