DEV Community

Paulo GP
Paulo GP

Posted on

Python: A Guide to Lambda Functions

Introduction

In this chapter, we'll explore the use of lambda functions in Python, using a math theme to illustrate their functionality. Lambda functions, also known as anonymous functions, are a way to create small, one-time-use functions in Python. They are often used in situations where a short, simple function is needed, such as when passing a function as an argument to another function.

Creating a Simple Lambda Function

Let's start by creating a simple lambda function that takes two arguments and returns their sum:

add = lambda x, y: x + y
Enter fullscreen mode Exit fullscreen mode

In this example, we create a lambda function that takes two arguments x and y and returns their sum. We assign this lambda function to a variable add so that we can use it like any other function:

result = add(3, 4)
print(result)
Enter fullscreen mode Exit fullscreen mode
Output:
7
Enter fullscreen mode Exit fullscreen mode

Using Multiple Arguments

Lambda functions can also take multiple arguments. For example, let's create a lambda function that calculates the average of three numbers:

average = lambda x, y, z: (x + y + z) / 3
Enter fullscreen mode Exit fullscreen mode

In this example, the lambda function average takes three arguments representing three numbers, and returns their average. We can call this lambda function by passing three arguments inside the parentheses:

result = average(3, 4, 5)
print(result)
Enter fullscreen mode Exit fullscreen mode
Output:
4.0
Enter fullscreen mode Exit fullscreen mode

Using Lambda Functions with Map and Filter

Lambda functions are often used as arguments to other functions, such as the map and filter functions. For example, let's say we have a list of numbers and we want to create a new list with the squares of these numbers. We can use the map function with a lambda function to achieve this:

numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, numbers))
print(squares)
Enter fullscreen mode Exit fullscreen mode
Output:
[1, 4, 9, 16, 25]
Enter fullscreen mode Exit fullscreen mode

In this example, we use the map function to apply the lambda function to each element of the numbers list. The lambda function takes one argument x and returns its square. The map function returns an iterator, so we need to convert it to a list to see the result.

We can also use the filter function with a lambda function to filter a list based on a condition. For example, let's say we want to create a new list with only the even numbers from the original list. We can use the filter function with a lambda function to achieve this:

numbers = [1, 2, 3, 4, 5]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
Enter fullscreen mode Exit fullscreen mode
Output:
[2, 4]
Enter fullscreen mode Exit fullscreen mode

In this example, we use the filter function to apply the lambda function to each element of the numbers list. The lambda function takes one argument x and returns True if it is even, and False otherwise. The filter function returns an iterator with only the elements for which the lambda function returns True, so we need to convert it to a list to see the result.

Conclusion

Lambda functions offer a concise and flexible way to create small, one-time-use functions in Python. They can be used to perform simple calculations, manipulate data, and pass functions as arguments to other functions.

Top comments (0)