In this post, we’ll learn what Python lambda function is and how to properly use it in code.
We’ll be using multiple practical examples so you can have a better idea of how Python lambda function works.
After reading this post, I’m sure you’ll be able to easily incorporate Python lambda function in your own code as well.
So let’s not delay anymore and just dive right into its practical usage.
What is Python Lambda Function?
It is also called an anonymous function.
Unlike other Python functions, lambda function is defined without a name.
Other functions starts with def keyword while Python lambda function starts with lambda keyword.
Let’s first understand the syntax of this anonymous function. After that, we’ll move forward to practically implement it using proper Python code examples.
Syntax of Lambda Function
lambda arguments:expression
Explanation
- It starts from the keyword lambda.
- Arguments can be multiple.
- Colon(:) should be used after the last argument.
- Expression should only be one and it’ll be returned after execution.
Implementing Python Lambda Function (Multiple Code Examples)
See below examples to understand how lambda function can be implemented practically.
Example 1: Lambda Function with One Argument
val = lambda arg: arg*2 print(val(10))
Output
20
You can see that we have used a lambda function having one argument which will be multiplied with 2 and the returned function object will be stored in val.
For demonstration, we have passed a value 10 to this lambda function and as a result, we get an output of 20 which means that Python lambda function is working successfully.
Example 2: Lambda Function with Multiple Arguments
val = lambda x,y,z: x*y*z print(val(2,3,6))
Output
36
You can names the arguments of your choice. We’ve used three arguments here and have passed some values to them. As a result, the function got executed and the expected value is returned.
Example 3: Lambda Function inside Map Function
Lambda function can be used inside Python’s map function. Map functions takes two arguments, a function and a list.
In our example, we’ll pass a lambda function to this map function and also a custom list. And we’ll pass this function of map to Python’s list function. So it can create a new list with modified items.
We will multiply each item of the custom list with 2 using the lambda function and return a new list. See below code:
listItems=[1,2,3,4,5,6,7,8,9,10] val = list(map(lambda y:y*2,listItems)) print(val)
Output
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Example 4: Lambda Function inside Filter Function
We can also use the same above process to filter items from a list. For that, we have to use the Python’s filter function, then we have to pass lambda function and a custom list to it as it also takes two arguments, a function and a list. See below code:
listItems=[1,2,3,4,5,6,7,8,9,10] val = list(filter(lambda y:y%2==0,listItems)) print(val)
Output
[2, 4, 6, 8, 10]
You can see that we’ve specified a condition in Python lambda function that return only those items that are even.
So this is how you can easily use Python lambda function in your own code with ease.
Do ask if you still have any questions related to the implementation of Python lambda function. I’ll be very glad to answer all your questions.
Conclusion
To conclude, hope you now have an in depth practical understanding of how to easily and properly use Python lambda function. I’ll be looking forward to have your valuable feedback on this post. Thank you for reading it.