Lambda functions in Python are functions that are anonymous. They do not need to be named. In this post, we will see how to define and use lambda functions. We will also see the advantages of lambda functions over normal functions in Python.

Defining a Lambda function in Python

Lambda function is just a fancy name for functions that don’t have a name. They can have multiple arguments, like any other function, but they can only have one expression.

To define a lambda function, we use the syntax as shown below:

lambda argument(s): expression

Let’s see an example.

Output:

6

We just wrote a lambda function to double the value passed as an argument.

Didn’t we just say that lambda function is anonymous and isn’t double the name of the function here? However, it isn’t the case. Note that the statement lambda n : n * 2 is the whole function. We are assigning the function object to the variable double. You could just as well write this:

Also, when you inspect the lambda function:

Output:

<function <lambda> at 0x7fb4cce3ae18>

Did you notice that it shows <lambda> instead of the function name?

Contrast that to the normal function define using def .

<function double at 0x7fb4ccd7f8c8>

Multiple Arguments

Let’s see one more example with multiple arguments.

Output:

15

When we want to pass multiple arguments, we join them with a comma.

Advantages of Lambda functions over def functions

You might be wondering, if lambda function behaves the same way as def functions, why do I need to learn about lambda functions? However, there are some scenarios where lambda functions are more useful than the normal functions.

Following are some benefits of lambda functions:

  • Lets you create a function inside a function
  • Useful for functional programming, when used along with map(), reduce() and filter() function
  • Can be used with sorted function to define an alternate sorting key.

Let us see some of the usage of lambda function.

1. Function inside a function

You can use a lambda function when you want to pass a function as an argument to another function. For example, consider a scenario when you want to multiply a number to itself for multiple times but don’t know the exact number. So, you could define a function this way:

Here, we are defining a function that calculates the value of a number when raised to the power of another number. Using this, we can define and use multiple lambda functions:

Output:

9
27

2. Map, Reduce and Filter

The real power of lambda functions are unleashed when used along with map, reduce and filter functions.

map() function

Map() is used when you have a list of items on which a same kind of operation is to be performed. For example, when you have a list of items and you want to multiply all the items by 3. You can do this in a single line using map() and lambda function.

Output:

[12, 15, 18]

reduce() function

The reduce() function comes in handy when you want to perform an operation on all the items in a list. Suppose, you want to add all the items in a list. Without a lambda function, the way you would do it is:

However, using the reduce() function, we can make it way simpler:

filter() function

The filter() function is useful when you want to get the list of items for which a function returns True. For example, you want to filter out the odd numbers from a list, you can do it this way:

3. Defining an alternate sorting key

Let’s say you have a list of names:

You want to sort the names based on the first names:

Output:

['Daren Mascia', 'Elmo Jeong', 'James Stewinsky', 'Jason Mai', 'Matt Broomhall']

Pretty easy? Now, you want to sort the list based on the last names? How would you do it? Lambda makes things easy here.

Output:

['Matt Broomhall', 'Elmo Jeong', 'Jason Mai', 'Daren Mascia', 'James Stewinsky']

See also: Comprehensions in Python

*****

If you have any questions or comments regarding the post (or something else), please feel free to reach out through the comments.