Functions of Lambda in Python
As discussed in the previous module, we know that there are three types of Python Functions. One of them is an anonymous function. Anonymous functions are functions without a name. Now, to define a normal function, we use the keyword, ‘def’. Similarly, to define an anonymous function, we use the keyword, ‘lambda’. Since anonymous functions are defined using the lambda keyword, they are also sometimes referred to as lambda functions.
Watch this ‘Lambda Function in Python’ video:
In this module, we will learn all about lambda functions in Python in order to get started with them. Following is the list of all topics covered in this module.
So, without any further delay, let’s get started.
Interested in learning Python? Check out the Python Training
What is a Lambda Function in Python?
The lambda keyword is used to define anonymous functions, that is, functions without names. Python lambda functions are not much different from the regular functions that are defined using the def keyword.
Syntax of a lambda function in Python:
lambda arguments: expression
Here’s an example of Lambda in Python.
(lambda a,b: a+b) (4,6)
If we execute the above code line, the output will be 10.
In the above example, we simply performed an addition operation using the lambda function in Python. If we compare this example to the syntax of the Python lambda function, a and b are the arguments and a+b is the expression that is being evaluated and returned, and the whole statement is the lambda function. We have passed the values in arguments as soon as we defined the lambda function (values being 4 and 6, respectively). The same operation can be performed using a regular function as shown below:
Now, the question here is that if we can perform the same operation using a regular function, then why do we need a lambda function in Python? Moving forward, let’s find out why we need Python lambda functions at all.
Become a master of Python by going through this online Python Course in Toronto!
Why Use Lambda Functions in Python?
A lambda function is not an absolute necessity in Python, but using a lambda function in certain situations definitely makes it a bit easier to write the code. Not just that, it also makes the written code a bit cleaner. Now, in what all situations using a lambda function is beneficial? Following are some of the situations where using a lambda function is preferred.
- Lambda functions in Python are very useful in defining the in-line functions where the regular functions, defined using the def keyword, won’t work syntactically. Since a lambda function is an expression rather than a statement, it can be used in places where a regular function is not allowed by the Python syntax, for instance, in places such as inside a Python list or in a function’s call argument.
- As observed in the above example, the same operation performed by a regular function with the function body of at least three to four lines can be performed by a lambda function that only takes one line. Then, why use a long function to perform a simple operation when it can be done in a single line expression?
So, to summarize, a lambda function behaves like a regular function, takes an argument, and returns a value but is not bound to any name or identifier. There is no need to use the return statement in a lambda function in Python; it will always return the value obtained by evaluating the lambda expression in Python.
Want to learn Python from an Expert? Enroll in our best Python training in Bangalore and learn from Professional Industrial Expert.
Properties of Python Lambda Functions
- Anonymous functions created using the lambda keyword can have any number of arguments, but they are syntactically restricted to just one expression, that is, they can have only one expression.
- The Lambda function in Python can be used wherever a function object is required.
- Lambda functions do not require any return statement; they always return a value obtained by evaluating the lambda expression in Python.
- Python Lambda functions are widely used with some Python built-in functions such as map(), reduce(), etc.
Get certified by this top Python Course in Singapore today!
Lambda Function with map() in Python
There is a function in Python, map(), which takes a function and a list as arguments. When called with the lambda function, map() function returns a new list that contains all the lambda modified items that are returned by that function for each item.
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(map(lambda x: x*2, li))
The output will be [10, 14, 44, 194, 108, 124, 154, 46, 146, 122]
Lambda Function with filter() in Python
There is another function in Python, filter(), which also takes in a function and a list as arguments. This gives you an easy way to filter out all the elements of a sequence, for which the function returns True.
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(filter(lambda x: (x%2 != 0) , li))
The output will be [5, 7, 97, 77, 23, 73, 61]
With this, we come to the end of this module on Python Tutorial. Here we learn about what is Lambda in Python. And how to use the Python Lambda function with the help of examples. We also learned why we would use them. Further, we talked about the properties of lambda functions in Python.
Now, if you want to know why Python is the most preferred language for Data Science, you can go through this blog on Python Data Science tutorial.
Check out how Intellipaat is helping people get Python training in the easiest way. And also, we are providing trainees with a free guide to all Python interview questions.