Lambda Expression, Map and Filter

Let’s learn about the lambda expression and its use with map and filter methods.

Lambda expression

Now that we know the basics of functions, we can move on and learn about the lambda expression. We’ll be using lambda expressions extensively in our pandas library.

  • A lambda expression can be used to create a small anonymous function, that is, a function without any name.
  • These throw-away functions are created where we need them.
  • Lambda expressions are mainly used in combination with the filter() and map() built-in functions.

The general syntax of a lambda expression is:

lambda argument_list: expression

In the syntax above, argument_list consists of a comma-separated list of arguments and expression is an arithmetic expression that uses these arguments.

We can assign a lambda expression to a variable to give it a name as well.

Let’s write a function, which returns the square of a number, and then rewrite that function as a lambda expression. This is easier than it sounds!

Get hands-on with 1400+ tech skills courses.