👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
- Python lambda function or Python anonymous function has no name.
- We can define an anonymous function using the reserved lambda keyword.
- The scope of an anonymous function is limited to the current scope in which it is defined.
- A lambda function can have one or more arguments, but it can only have one expression.
- The expression is evaluated and the result is returned from the lambda function.
- Typically used with map (), filter (), and reduce () operations.
Python lambda function syntax
Lambda function syntax:
lambda arguments: expression
Anonymous function example
Let’s say we have a function to get the area rectangle.
def area_of_rectangle (l, w): return l * w print (f’Area of ‚Äã‚ÄãRectangle (4, 5) is {area_of_rectangle (4, 5)} ’)
Let’s create an anonymous function using the lambda keyword to get the area of ‚Äã‚Äãthe rectangle.
rectangle_area = lambda x, y: x * y print (f’Area of ‚Äã‚ÄãRectangle (4, 5) is {rectang le_area (4, 5)} ’)
When to use?
- For small trivial dach with little complexity.
- When a function has only one expression.
- For repetitive tasks that are temporary.
- If you want the scope of the function to be limited to the current scope only.
- This is useful when the function argument is another function, such as the map (), filter (), and reduce () functions.
Lambda function with map ()
The map () function takes a function and an iteration as arguments. The function is applied to each element in the iterable, and the updated iterable is returned.
Let’s say we have a list of integers. We have to create a new list by multiplying each element by 10. We can use a lambda function here instead of creating a function for this single use case.
list_numbers = [1, 2, 3, 4] list_numbers = map (lambda x: x * 10, list_numbers) for num in list_numbers: print (num, end =" ")
Output:
With filter ()
The built-in filter () function takes a function and an iteration as an argument. The function is applied to each element of the iteration. If the function returns True, the element is added to the returned iteration.
Let’s say we have a list of integers and we want to remove all odd numbers. The final list should only contain integers.
list_numbers = [1, 2, 3, 4, 5, 6] list_numbers = filter (lambda x: x% 2 == 0, list_numbers) for num in list_numbers: print (num, end = "")
With reduce ()
The reduce () function is present in the functools module. This function takes a function and a sequence as an argument. The function must take two arguments. The elements from the sequence are passed to the function along with the accumulated value. The end result &one value.
Let’s say we have a list of integers and we want to get the sum of all its elements.
from functools import reduce list_ints = [1, 2, 3, 4, 5, 6] total = reduce (lambda x, y: x + y, list_ints) print (f’Sum of list_ints elements is {total} ’)
Python lambda function with no arguments
Yes, we can define a lambda function with no arguments. But it is useless, because there will be nothing to operate on. Let’s see a simple example.
get_5 = lambda: 5 print (get_5 ()) # 5
Since the lambda the function always returns the same value, we can simply assign a variable to it. Using without arguments &clear abuse of this feature.
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Python lambda functions, check other accumulate-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Python lambda functions
- Deutsch Python lambda functions
- Français Python lambda functions
- Español Python lambda functions
- Türk Python lambda functions
- Русский Python lambda functions
- Português Python lambda functions
- Polski Python lambda functions
- Nederlandse Python lambda functions
- 中文 Python lambda functions
- 한국어 Python lambda functions
- 日本語 Python lambda functions
- हिन्दी Python lambda functions
London | 2023-03-25
I was preparing for my coding interview, thanks for clarifying this - Python lambda functions in Python is not the simplest one. I just hope that will not emerge anymore
Massachussetts | 2023-03-25
Maybe there are another answers? What Python lambda functions exactly means?. Will use it in my bachelor thesis
Shanghai | 2023-03-25
I was preparing for my coding interview, thanks for clarifying this - Python lambda functions in Python is not the simplest one. I am just not quite sure it is the best method