In Python, a lambda function is a one-line, anonymous function defined using the lambda keyword. Unlike regular functions defined with def, lambda functions are typically used for short, simple operations that are used only once or for a short time.
What Is a Lambda Function in Python?
A lambda function is a small, anonymous function defined in Python using the lambda keyword. It can take any number of arguments but is limited to a single expression, and the evaluated expression result is automatically returned. Lambda functions are typically used for simple operations and are often passed as arguments to other functions.
In cases of applying a calculation to all aspects of a data set at once or needing to pass a function as a parameter to another function, a lambda function in Python can be helpful.
What Is a Lambda Function?
A lambda function is a compact, single-line function often called an anonymous function because it doesn’t require a formal name. Defined with the lambda keyword in Python, it’s a concise alternative to a full def declaration.
Lambda functions are most frequently used when you need to pass a simple operation as an argument to a higher-order function (a function that accepts another function as input). This is a common and powerful pattern in Python and Pandas, particularly for operations like applying a calculation to a data set with methods such as pandas.apply().
How Does a Lambda Function in Python Work?
Because a lambda function is a single-line expression, its syntax is very specific: lambda argument: expression. The arguments are the inputs to the function, and the expression is the calculation performed. The function’s return value is the result of that expression.
That line has the following three features:
- lambda: This is the keyword informing Python that the following information is a lambda function. Since this is a keyword with a specific meaning to Python this term cannot be changed.
- argument(s): This is the input to a function, and is similar to the x in function(x). This tells the lambda function what value to modify in the next part of the definition. The argument(s) can be whatever value you desire. Since this is a temporary function many people don’t worry about specificity as much and simply use
xas a convention. Others are sticklers for clarity in code (I happen to be in that camp) and choose to use descriptive names. -
expression: This is where you define the expression to be applied to the data set. If you want to return the square of numbers your expression could be
x ** 2. Or if you want to calculate the sales tax, (assuming six percent) on items with known sales value (and are a stickler for clarity) your expression could besales_value * 0.06.
Note the variable name you use for the expression (in this case either x or sales_value) must match what’s listed in the argument. So, in this case, that would have to be x or sales_value depending on which method you use.
Lambda Function in Python Example
As an example of using the lambda function in Python, let’s first write a function that accepts another function as an input. This function will be called apply_to_list and will behave similarly to the pandas.apply() function, but will work specifically on lists (I’ll simplify for the sake of the example).
def apply_to_list(list, f):
return[f(x) for x in list]
This function accepts both a list and a function as an input. The function then uses a list comprehension to evaluate that function on each value in the list, stores that value in a new list and, after executing the function for all values in the list, returns a new list with the modified data.
Now we have a function that can apply any function we create to a list. This is an excellent opportunity to use a lambda function to perform some calculations. Let’s stick with our example of calculating sales tax based on known sales value.
First, we create a list of data representing the value of different sales, then we pass it to apply_to_list as a lambda function. You can do this as follows:
def apply_to_list(list, f):
return[f(x) for x in list]
sale_value = [18, 24,5, 17, 21, 19]
sales_tax = apply_to_list(sale_value, lambda x: x * 0.06)
print(sales_tax)
#Output: [1.08, 1.44, 0.3, 1.02, 1.26, 1.14]
This will calculate the sales tax for each item in sale_value, store it in a new list, and print that new list to the terminal. For this particular example the output will be: [1.08, 1.44, 0.3, 1.02, 1.26, 1.14].
One weakness in this implementation is that I hard-coded the sales tax to be six percent in the lambda function. A more flexible implementation would define the sales tax rate as a variable and reference that variable in the lambda function.
Anonymous functions are a great way to simply write a function that you only need to reference one time, especially when passing it to a different function as an input. Now you know how to write anonymous functions and can use them as inputs to other functions. Happy coding!
Frequently Asked Questions
What is a lambda function in Python?
A lambda function in Python is a small, single-line function defined with the lambda keyword. It is often called an "anonymous function" because it does not require a formal name.
Why would I use a lambda function instead of a regular function?
In Python, lambda functions are useful when you need to apply a simple calculation to a data set at once or when you need to pass a function as an argument to another function. They are more compact and don't require formal declaration.
What is the basic syntax for a lambda function?
The syntax for a lambda function in Python is lambda argument: expression.
This includes the keyword lambda, a placeholder for the argument(s) or input value(s) and the expression to be applied to that input.
