All about Python Lambda (Anonymous) FunctionPython Tutorials
Have you ever faced the challenge of not being able to remember a function or variable name while coding?
You must have had to scroll to the top of the code to find that name so you could use it. You may have even used the same name for two different variables, which must have caused a lot of confusion. These problems can occur quite often, especially in complex codes like the one shown above. Would it not be convenient if you could use a function only one time, execute a command with it, and then forget all about it? Fortunately, you can do just that by using the Python lambda (anonymous) function.
Python and other programming languages like Java, C++, and C# have small anonymous functions called lambda meant for one-time use. While we use the def keyword to define a function in Python, we use the lambda keyword to define an anonymous function. As the name suggests, the function that is defined using this keyword will have no name. Since we use the lambda keyword, we also call these functions as lambda functions.
Here at Fixwebnode, as part of our Web development Services, we regularly help our customers to perform related Python functions queries.
In this context, we shall look into what Python lambda (anonymous) function is and how it works.
Table of contents [Show] [Hide]
- 0. 1 What is the syntax for Python lambda (anonymous) function ?
- 0. 2 What are the Characteristics of the Lambda Function ?
- 0. 3 How to use Lambda Functions in Python ?
- 0. 4 How to use Built-in Functions like Filter() and Map() with Python Lambda (anonymous) Function ?
- 0. 5 [Need to develop your website today? Let us know. ]
- 1 Conclusion
What is the syntax for Python lambda (anonymous) function ?
If you want to use the Python lambda (anonymous) function, you will have to follow this syntax:
lambda [arguments] : expression
The lambda function can have any number of arguments, or no arguments at all, after the colon (:). When you call the lambda function, you will see that the expression after the ':' is executed.
What are the Characteristics of the Lambda Function ?
The main characteristics of the Lambda Function is outlined below:
- The function can take any number of arguments (numbers, letters, words, etc.) but can only have one expression.
- The function can be used anywhere in the code where functions are required.
- They are syntactically restricted to a single expression. That means, you cannot use loops or a conditional statement like for, while, if, else, etc., with a lambda function.
How to use Lambda Functions in Python ?
Let's take a look at the following example:
square = lambda a : a * a
Here, we want to execute a program to find the square of a number. We start the function by using the lambda keyword and follow it by giving it an argument 'a'. The expression in the above code (a*a) will return the square value of a when we call the function.
We assign the entire lambda function to the variable 'square' as shown below:
The output that will be displayed will be:
The variable name becomes the function name in the above example, and we can use it as a regular variable.
In another example, your will see the difference between defining and calling a normal function and a lambda function.
Let's calculate the cube of a number:
# Python code for cube of a number
# Using def keyword
# Using lambda keyword
l_cube = lambda c: c*c*c
# using a normally defined function
# using the lambda function
When executed, its output will look like this:
You will notice that both the cube() function and the l_cube() function perform the same calculations as we expected.
Both the functions return the cube of a number. However, we had to define a named function (cube) using the def keyword in the first code and then pass a value in it. We also had to use the return keyword to return the result from where we called the function.
On the other hand, when using the lambda function, we did not have to use the return keyword as it always contains an expression that is returned.
Hence, we use the lambda function or the anonymous function to simplify the code into one-line expressions. We also use them when we require a function for a short period of time. The simplicity of the function also allows us to use it anywhere it is expected, without having to assign a variable to it.
How to use Built-in Functions like Filter() and Map() with Python Lambda (anonymous) Function ?
1. The Filter() Function
In Python, the filter() function takes in a function and a list as arguments. This gives us an easy way to filter all the elements in a sequence. When the lambda() function is passed in the filter() function (with the list to be evaluated) a new list is returned that contains items. Take a look at the following example:
# Python code to use filter() with lambda()
lis = [5, 7, 22, 9, 54, 6, 77, 2, 73, 66]
final_li = list(filter(lambda x: (x%2 != 0) , li))
Here, a list of random numbers is assigned to the variable 'lis'. We then call the filter() function and pass it as an argument in the lambda function. The function divides each number by 2 and returns "True" if the number is not equal to 0. We use the print function to print the new list.
When you run it, the output will look like this:
[5, 7, 9, 77, 73]
2. The Map() Function
The map() function is similar to the filter() function as it takes in a function and a list. However, while the filter() function only prints values that are true, the map() function prints a new list that contains items returned by the function for each item. Let us look at an example:
# Python code to use map() with lambda()
# to double each item in a list
li = [5, 7, 25, 97, 82, 19, 45, 23, 73, 57]
final_li = list(map(lambda x: x*2, li))
Here, we have first created a list of random numbers called li. We then pass the map() function in the lambda() function. The expression in the lambda function multiplies every item in the list (doubles) and returns its value to final_li.
When you run this code, you will get the following output:
[10, 14, 50, 194, 164, 38, 90, 46, 146, 114]
[Need to develop your website today? Let us know. ]
This article covers the most important part of Python lambda (anonymous) function which is a no-name function declared in a single line. It can have only one expression and is used when a short-term function is required. It is defined using the lambda keyword and is similar to a regular function (defined by using the def keyword). We pass the lambda function as an argument in another function, as well.
What is lambda in Python?
In Python, lambda is a keyword that is used to define a lambda function. You do not have to assign a name to these lambda functions, and you can use them anywhere in the Python code. The lambda expressions in Python find their root in lambda calculus, which is a complex computing language created by Alonzo Church in the 1930s.
Why is lambda used in Python?
Coding can sometimes be very confusing as we use a lot of variables and functions to execute tasks. The lambda function helps us reduce the code into one-line expressions. They work the same way as a normal function and give us the same results. However, they do not take up as much space and can be used for one-time executions.
How does lambda work in Python?
To use the lambda in Python, you will have to follow the following syntax:
lambda [arguments] : expression
The lambda keyword in the above syntax is used to define a lambda function. The function can take any number of arguments, which may be integers, strings, float, etc. However, there can only be one expression after the ':'. That means you cannot use a loop or conditional statements in the lambda function.
Lambda Function in Python – How and When to use? Lambda Function, also referred to as 'Anonymous function' is same as a regular python function but can be defined without a name. While normal functions are defined using the def keyword, anonymous functions are defined using the lambda keyword. However, they are restricted to single line of expression. They can take in multiple parameters as in re