Before delving into the syntax and usage of Lambda functions with specific examples, let's start by understanding the definition of Lambda functions in Python.
1. Decoding LAMBDA
In Python, a Lambda function is an anonymous or nameless function, defined without a name. It's a concise and limited function, not exceeding a single line. Similar to a regular function, a Lambda function can have multiple parameters with a single expression.
In Python, the Lambda expression is used to construct anonymous functions. To do so, you'll use the lambda keyword (similar to using def for defining regular functions). Each anonymous function you define in Python will have three essential parts:
- A Lambda function can take any number of arguments, but they only contain a single expression. The expression is a piece of code executed by the Lambda function, which may or may not return any value.
- Lambda functions can be used to return function objects.
- Syntax-wise, Lambda functions are confined to a single expression.
2. Utilizing Lambda Functions in Python
A Python Lambda function has the following syntax:
2.1. Syntax of Lambda Function
Here, you can add as many parameters as needed. However, note that we don't use parentheses around parameters as we do with regular functions. The expression can be any valid Python expression that operates on the parameters you provide to the function.
2.2. Example of Lambda Function in Python
Here's an example of a Lambda function doubling the input value:
In this example, lambda x: x * 2 is the Lambda function. x is the parameter, and x * 2 is the expression responsible for computation and returning the result.
This function is anonymous. It returns a function object associated with the identifier double. We can call it like a regular function. The command
double = lambda x: x * 2
is akin to:
def double(x):
return x * 2
In addition to using LAMBDA functions in Python, users can employ this function in Excel to create custom functions. Details can be found here.
- Explore more: Users can now create custom Excel functions with LAMBDA.
3. Using Lambda Functions in Python
Typically, Lambda functions are used when a temporary anonymous function is needed. In Python, we use it as an argument to higher-order functions (functions that take other functions as arguments). Lambda functions are employed in conjunction with built-in functions such as filter(), map(), or reduce()...
3.1. Example Using Lambda Function with filter()
The filter() function in Python takes parameters in a function or a list. The function will be called with all items in the list, and a new list is returned, containing items that the function evaluates as True.
Here's an example of using filter() to selectively filter even numbers from a list:
3.2. Example Using Lambda Function with map()
The map() function in Python also takes parameters in a function and a list. The function is called with all items in the list, and a new list is returned, containing items that the function returns for each item.
Here's an example of using map() to double all items in a list.
4. Why Use Lambda Functions?
Lambda functions are treated similarly to regular functions at the interpreter level. In a way, you could say that Lambda functions provide concise syntax for writing functions that return a single expression.
One of the most common use cases for Lambda functions is in functional programming because Python supports a programming paradigm called functional programming.
It allows you to provide a function as a parameter to another function (e.g., in map, filter...). In such cases, Lambda functions offer an elegant way to create a one-time-use function and pass it as a parameter.
