How to Use Python Lambda Functions
- 2020-03-10 09:35 AM
In this Python tutorial, you'll learn about Python lambda functions. You'll see how they compare with regular functions and how you can use Python lambda Functions
In Python, a Lambda Function refers to a small, anonymous function. We call the functions anonymous because technically it has no name — we don’t define it with the standard
def keyword that we normally use in Python. Instead, Lambda Functions are defined as one-liners that execute a single expression.
Although they look different, Lambda Functions behave in the same way as regular functions that are declared using the
def keyword. They are executed in a similar way as regular Python functions, with the exception that they strictly execute a single expression.
Lambda functions are mainly used for creating small, single-use functions. You’ll often see them in-place of what might otherwise be a fully defined function, but written as a Lambda to save time and space. From the Python Design and History FAQ:
Unlike lambda forms in other languages, where they add functionality, Python lambdas are only a shorthand notation if you’re too lazy to define a function.
For a concrete description, Lambda functions can be understood through the following 3 points:
- A Lambda Function must always execute a single expression
- An expression is a Python code run by the lambda function, which may or may not return any value
- A lambda function can take any number of input arguments and return any number of output arguments, as long as the function is maintained as a single expression
Basic Lambda Functions in Python
All Python Lambda Functions following the same exact syntax:
lambda arguments: expression
For example, let’s say we want to declare a Lambda Function that computes the remainder of a division operation. Of course, we could do this without a function with Python’s
% operator, but it’s not very readable when going through the code. It’s easy to miss and not intuitive to catch when reading through for the first time.
When we use a Lambda Function however, we’re able to clean things up for better readability and cleaner code:
compute_remainder = lambda x, y: x % y
Our Lambda Function takes on 2 arguments,
y, and then computes the remainder of those 2 using Python’s
% operator via
x % y. To call the function, all we have to do is apply it like any other regular Python function by passing the arguments and saving the return value in a variable.
### Prints out 1 r = compute_remainder(10, 3) print(r)
Our code using the Lambda Function is simple and contained.
The filter() Function
filter() function is used to select specific elements from a Python sequence. A Python sequence is a data type that can hold multiple elements such as a list, tuple, set, or string.
The filter function will take two items as an input, an object and a sequence:
The object will be our Python Lambda Function. Thus, we are filtering the sequence using our Python Lambda function.
Since the Lambda Function we are passing will be performing a filtering operation, we’ll need it to strictly return a single boolean value —
True to keep an element and
False to filter it out. The filter function will then return another sequence only containing the elements that evaluated to True by the Lambda.
For example, let’s say we have a list of numbers in which we want to filter out all numbers which are less than 50. We can do this with list comprehension:
### Prints out [66, 91, 98, 80, 99] data = [66, 15, 91, 30, 35, 38, 43, 20, 38, 28, 98, 50, 7, 80, 99] filtered = [d for d in data if d > 50] print(filtered)
While list comprehension is definitely Pythonic, it’s not very reader-friendly. Instead, the filter function with a Lambda will look a lot cleaner.
### Prints out [66, 91, 98, 80, 99] data = [66, 15, 91, 30, 35, 38, 43, 20, 38, 28, 98, 50, 7, 80, 99] filtered = list(filter(lambda x: (x > 50), data)) print(filtered)
Our filter fits nicely into a one-liner while being more readable than list comprehension and accomplishing the same task.
The map() Function
map() function is used to apply an operator or expression to every element in a sequence. For example, if your sequence is a list of numbers, you might want to divide each number by 2, which is a great situation to use the
The map function will take two items as an input, an object and a sequence:
Just like before, the object will be our Python Lambda Function. Thus, we are executing the expression in our Lambda to each element in the sequence.
Since the Lambda Function we are passing will be executing an expression on each list element, our Lambda can return any type of value. The only thing to really keep an eye out for is that your list doesn’t have any elements that would throw an error at your Lambda function. For example, you might have a list that contains both integers and strings, so attempting to divide each element by 2 wouldn’t work.
Let’s use a similar example to the one we used with
filter() we had a list of numbers. In this case, we’re going to write code to square each element in the list. Again, we can always do this with list comprehension:
### Prints out [33.0, 7.5, 45.5, 15.0, 17.5, 19.0, 21.5, 10.0, 19.0, ### 14.0, 49.0, 25.0, 3.5, 40.0, 49.5] data = [66, 15, 91, 30, 35, 38, 43, 20, 38, 28, 98, 50, 7, 80, 99] mapped = [(x / 2) for x in data] print(mapped)
While list comprehension is definitely Pythonic, it’s not very reader-friendly. Instead, the map function with a Lambda will look a lot cleaner.
### Prints out [33.0, 7.5, 45.5, 15.0, 17.5, 19.0, 21.5, 10.0, 19.0, ### 14.0, 49.0, 25.0, 3.5, 40.0, 49.5] data = [66, 15, 91, 30, 35, 38, 43, 20, 38, 28, 98, 50, 7, 80, 99] mapped = list(map(lambda x: (x / 2), data)) print(mapped)
Our mapping fits nicely into a one-liner while being more readable than list comprehension and accomplishing the same task.
So there you have it! Your practical introduction to Python Lambda Functions.
If you’re hungry for more, not to worry! There’s a whole lot more to learn about the many intricacies of Python. As recommended reading, the Real Python is a great resource with many in-depth tutorials. If you want to learn more about Python in general, Coursera has a Python for Everybody course that’s more application focused.