Get startedGet started for free

Lambda functions

1. Lambda functions

We've worked with custom functions. Now, let's explore an approach that offers flexibility without the need to build a complete function from scratch.

2. Simple functions

Custom functions are great, but can require a lot of code! As an example, this simple function calculating the average from a sequence of values requires three lines of code - one to define the function, one for the function body, and another to return an output. However, there is a quicker way to achieve the same result!

3. Lambda functions

We can use the lambda keyword to create an anonymous function, which doesn't require a name or need to be saved as a variable. The general syntax is lambda,

4. Lambda functions

followed by one or more arguments,

5. Lambda functions

a colon,

6. Lambda functions

and an expression. The convention is to use x for a single argument, although any word will work. The expression is the equivalent of the function body. Also, no return statement is required to produce an output! Lastly, while lambda functions can be used anonymously, storing and then calling them is also possible. Let's explore both approaches.

7. Creating a lambda function

Here, we write a lambda function to find the average value. It shows an output pointing to a lambda function. This is the equivalent of defining a custom function and displaying it without calling the function and providing an argument.

8. Using lambda functions

To use the lambda function, we place it inside parentheses.

9. Using lambda functions

Immediately after, we open parentheses again and provide a value representing x — in this case, a list of three values. Now, we see an output of six.

10. Storing and calling a lambda function

If we intend to use the lambda function more than once, we could store it as a variable, like here. We can then call the variable, average, like a regular function, and provide the list representing x. The result is the same!

11. Multiple parameters

We can extend lambda functions in a couple of ways. We can use more than one argument, such as raising x to the power of y. The values of two and three represent x and y respectively, producing an output of eight.

12. Lambda functions with iterables

We can use lambda functions to perform actions on values within an iterable, such as a list. To do this, we need to use lambda inside Python's built-in map function, which applies a function to the iterable we provide. Let's use this approach to capitalize every value in a list called names. We call map, first providing the lambda function which uses the string.capitalize method, followed by the iterable to apply the function to, names. Printing this shows a map object pointing to the memory location where this function is stored. To produce an output, we need to convert it to a data structure. As we are working with a list, we do this by calling the list function and providing our capitalize function inside. It returns all values in the names list with capitalized first letters!

13. Custom vs. lambda functions

So when should we use lambda functions over custom functions? If the function is complex, such as using if-else syntax, or will be used many times, we should make a custom one. In contrast, if we need to do something once or it's relatively simple, we should use lambda.

14. Let's practice!

Time to practice working with lambda functions!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.