Lambda Expression in Python
Python’s Lambda expression are quite controversial in the programming circles. Some find it elegant and useful, while others freak out with the name of it.
By the end of this article, you will be able to decide for yourselves whether you want them to be a part of your code or not. We’ll help you get a clear understanding of what Lambda functions/ expressions are. Also, you’ll learn why and how you should use them in your program.
Let’s look at how Lambda expressions fit into your code!
What is Lambda function in Python?
Lambda functions are anonymous or nameless. Unlike traditional functions, lambda functions have no name. You can use anonymous functions when:
a. You want to perform a simple operation and,
b. You’re going to use this function just once.
Syntax of Python Lambda expression
lambda parameters: expression
The syntax of lambda expression contains three parts:
- lambda – This is the keyword used to create a lambda function
- parameters – A lambda function can have zero, one or many parameters. You just need to specify comma-separated parameters after the lambda keyword.
- expression – This is the body of the function. This is where you specify the operation performed by the function. A lambda function can have only one expression.
Python Lambda function Example
a. Lambda function with one parameter:
>>> lambda x: x**2
b. Lambda function with two parameters:
>>> lambda x, y: 2(x + y)
How to call a Lambda Expression in Python?
A lambda function always returns a function object. So you can either call the lambda function directly:
>>> (lambda x: x ** 2)(10)
Output
>>>
or you can assign it a name and then you can call it like a traditional function call:
>>> square = lambda x: x ** 2 >>> square(10)
Output
>>>
Lambda functions vs Traditional functions
Let’s look at the difference with the help of an example.
# traditional function to return the square of a number
def square(x): return x ** 2
# lambda function to calculate the square of a number
square_lambda = lambda x: x ** 2 print(square(10)) print(square_lambda(10))
All we’ve done here is that we’ve cleaned up some unnecessary syntax and changed some components. We use def to define a traditional function, whereas we use the lambda keyword to create a lambda function.
Also, in an anonymous function, we omit the return keyword, thereby further reducing the number of lines.
Other than these minor changes, let’s compare the two types of functions at a greater depth.
Traditional Functions | Lambda Functions |
They can have multiple expressions/ statements in their body. | They can have just a single expression in their body. |
These can have default values for their parameters. | Lambda parameters can’t have default values. |
Normal function can return an object of any type. | Lambda function always returns a function object. |
It takes more time for execution as compared to lambda functions. | These take relatively less time. |
Require more lines of code. | It requires a maximum of two lines of code to define and call a lambda function. |
How to use lambda Expression in your program?
One of the superpowers of lambda functions is that we can use them inside any normal function in Python. Thanks to the simplicity of lambda functions!
This comes handy while working with high-order functions, i.e., the functions that take another function as an argument.
Let’s have a look at two such functions – filter() and map().
Using lambda expression with filter()
The filter() function takes a function as its first argument. The second argument is an iterable, like, a list.
The filter() function then applies the argument function to every element of the iterable. It then returns a new iterable that contains all the elements for which the function evaluates to True.
Basically filter() filters out an iterable based on a function. It’s like adding an if condition to the iterable.
This is where we can make use of lambda functions.
>>> numbers = [10, 45, 23, 56, 6, 34, 78, 90, 3] >>> filtered_list = list(filter(lambda x: x > 50, numbers)) >>> print(filtered_list)
Ouput
>>>
Here the filtered_list contains all elements from the numbers list which are greater than 50.
Using Python lambda expression with map()
Similar to filter(), the map() function takes a function as its first argument. And, the second argument is an iterable, like, a list or a tuple.
The map() function calls the argument function on every element of the iterable. It then returns a new iterable that contains the elements returned by that function for each element of the original iterable.
We can use lambda functions here as well.
>>> numbers = [10, 45, 23, 56, 6, 34, 78, 90, 3] >>> new_numbers = list(map(lambda x: x + 2, numbers)) >>> print(new_numbers)
Ouput
>>>
The list new_numbers contains the elements of numbers added by 2.
Summary
This article comes to an end now. Hope you’ve got a better understanding of lambda functions through our article.
Lambda functions come in handy when you want to write quick “use-and-throw” functions. If you find yourself writing a simple function which you’re gonna use just once, think lambda! With Python, we have so many different features and concepts at our disposal.
It’s on us to understand them and use them to their full potential.