Anonymous function, Lambda in Python
We have gone halfway through the Python function, in this lesson you will learn about anonymous functions, also known as Lambda functions. What is Lambda jaw, how is Lambda function syntax and how to use Lambda function with specific example.
We have gone halfway through the Python function, in this lesson you will learn about anonymous functions, also known as Lambda functions. What is Lambda jaw, how is Lambda function syntax and how to use Lambda function with specific example.
What is Lambda function in Python?
In Python, anonymous functions are functions defined without names. If functions are normally defined using the def
keyword, the anonymous function is defined using the lambda
keyword. For that reason, the anonymous function is also called the Lambda function.
Lambda function syntax in Python
A Lambda function in Python has the following syntax;
lambda tham_so: bieu_thuc
Lambda function can have many parameters but only 1 expression. Expressions will be evaluated and returned. Lambda jaw can be used wherever the function object is required.
Lambda function example in Python
This is the Lambda function example with the task of doubling the number of entries.
nhan_doi = lambda a: a * 2
# Kết quả: 20
# By TipsMake.com
print(nhan_doi(10))
In this example, lambda a: a * 2
is Lambda function. a
here is the parameter and a * 2
is the expression (taking into account the calculation and returning the result). This function has no name, it returns a function object - assigned a nhan_doi.
We can call it as a normal function, the following command:
nhan_doi = lambda a: a * 2
almost like:
def nhan_doi(a):
return a * 2
Using Lambda in Python
Usually Lambda function is used when an anonymous function is needed in a short time, for example as an argument to a higher order function. Lambda function is often used with built-in Python functions such as filter () or map (), .
For example, using Lambda function with filter ():
The filter () function takes parameters as a function and a list. The function is called with all the items in the list and the new list will be returned, containing the items that the function evaluates to as True. This is an example of using the filter () function to filter out even numbers in the list.
list_goc = [10, 9, 8, 7, 6, 1, 2, 3, 4, 5]
list_moi = list(filter(lambda a: (a%2 == 0) , list_goc))
# Kết quả: [10, 8, 6, 2, 4]
# By TipsMake.com
print(list_moi)
For example, using Lambda function with map ():
The map () function also takes parameters as a function and a list. The function is called with all the items in the new list and list returned containing the items returned by the corresponding function for each item. Saying it seems ambiguous, you see the following example will be much easier to understand:
list_goc = [10, 9, 8, 7, 6, 1, 2, 3, 4, 5]
list_moi = list(map(lambda a: a*2 , list_goc))
# Kết quả: [20, 18, 16, 14, 12, 2, 4, 6, 8, 10]
# By TipsMake.com
print(list_moi)
Is there anything in the anonymous function that we missed? Please comment below to comment. Don't forget to do Python exercises and look forward to the next lesson.
Next lesson: Global variable (global), local variable (local), nonlocal variable in Python
Previous lesson: Python recursive function
You should read it
- Max () function in Python
- The next () function in Python
- The map () function in Python
- The oct () function in Python
- Zip () function in Python
- The ord () function in Python
- Functions in Python
- The function set () in Python
- The function id () in Python
- Int () function in Python
- Help () function in Python
- The function dir () in Python
Maybe you are interested
What does the 'super slow' 7680fps movie recording feature on the Mate 30 Pro do? Hitman Sniper famous game is being free on iOS and Android How to create slow motion videos on Android? 8 ways to win people's hearts 6 tips to help you get out of debt Changing these 10 ways of speaking will help you advance like 'windy kites'.