Python Lambda Functions

Click to share! ⬇️

Lambda expressions are a way to quickly create anonymous functions in Python. An anonymous function is like a one time use function that doesn’t have a name. You simply use it one time and then move on in the program. Lambda functions are typically used in combination with map() and filter(). The lambda function approach makes it convenient to quickly define a function in a shorthand form that can be used with these functions. In this tutorial, we’ll cover several examples of how to define a lambda function in Python, and how to make use of them efficiently.

From Function To Lambda

A Lambda Function is simply a shorthand version of a regular Python function. So to better understand how to make a lambda function in Python, we can convert a regular function to a lambda function in a step by step fashion. First, let’s look at a simple example of a standard Python function.

``````def plus_one(num):
result = num + 1
return result``````
``plus_one(7)``
`8`

Removing the `result` variable

Rather than taking the num variable, adding one, then storing it in the result, we can simply return that calculation.

``````def plus_one(num):
return num + 1``````
``plus_one(7)``
`8`

Making the function a one-liner

Now we can simplify the function further by having the calculation all on one line.

``def plus_one(num): return num + 1``
``plus_one(7)``
`8`

Remove the `def` keyword

Here we remove the def keyword along with the name and paranthesis we had assigned to our function (def plus_one()).

``num: return num + 1``

Remove the `return` keyword

Lambda functions do not have a return statement since it is implied with any lambda function.

``num: num + 1``

Add the `lambda` keyword

Lastly, we can add the lambda keyword in front of the stripped-down expression, and voila! We have a lambda function.

``lambda num: num + 1``

Assigning a Lambda to a variable

To make use of the lambda function, you often will use it in conjunction with other functions like map() or filter(). It is also commonly used with the reduce() function. We’ll look at that in a little bit. The first way you can use that lambda however is to simply assign it to a variable, and then use that variable as a function. Let’s see how that works.

``plus_one = lambda num: num + 1``
``plus_one(7)``
`8`

Using a `lambda` function with `map()`

The first argument to the map() function is always a function itself. It is referred to as a transformation function. Here is an example of a lambda with map().

``````nums = [1, 2, 3, 4, 5]

result = map(lambda x: x * 2, nums)

print(list(result))``````
`[2, 4, 6, 8, 10]`

Using a `lambda` function with `filter()`

The filter() function creates a list of elements for which a function returns true. It is often used with lambda expressions. Here is an example where we only want the numbers greater than 3.

``````nums = [1, 2, 3, 4, 5]

result = filter(lambda x: x > 3, nums)

print(list(result))``````
`[4, 5]`

Using a `lambda` function with `reduce()`

The reduce() function applies a rolling computation to sequential pairs of values in a list. A common example is to sum all of the values in a list, so let’s try that on our simple list of numbers.

``````from functools import reduce

nums = [1, 2, 3, 4, 5]

result = reduce(lambda x, y: x + y, nums)

print(result)``````
`15`

`if` statement with lambda

Can you use the Python if statement with lambda functions? Why yes you can. Let’s see an example of that.

``````result = map(lambda str: str.capitalize() if 'a' in str else str, 'abracadabra')

print(list(result))``````
`['A', 'b', 'r', 'A', 'c', 'A', 'd', 'A', 'b', 'r', 'A']`

Python Lambda Functions Summary

• Lambdas are one-line functions.
• Also known as anonymous functions.
• Often used when you don’t want to use a function twice in a program.
• Work just like normal functions and even behave like them.
• A lambda’s body is a single expression, not a block of statements.
• Quickly make ad-hoc functions without needing to properly define a function using `def`.
• lambda is designed for coding simple functions.
• A formal def function should be used for larger tasks.
Click to share! ⬇️