Lambda Function - Syntax and Examples
Published by
sanya sanya
Lambda functions, also known as anonymous functions, are a powerful and versatile feature of the Python programming language. They allow you to create small, anonymous functions on the fly, without the need for a separate definition. In this blog, we will explore what lambda functions are, how they work, and some common use cases.
Syntax of Lambda Function
lambda argument(s): expression
Lambda : Keyword used to define the anonymous function
Arguments : Variable that will contain the value you want to provide into the function expression. The 'arguments' parameter is a comma-separated list of the input parameters for the lambda function, just like the parameter list for a regular function.
Expressions : Consists of code which you want to run in the lambda function. The 'expression' parameter is a single expression that is executed when the lambda function is called, and its result is returned as the function's output.
As lambda function returns the outcome of the expression in the function , it does not have a return keyword.
At its simplest, a lambda function is a function without a name. Instead of defining a function using the 'def' keyword and giving it a name, you can define a lambda function using the 'lambda' keyword and pass it as an argument to another function, or use it in place of a named function in your code.
Examples of Lambda Function
-
Example of a lambda function that adds two numbers:
add = lambda x, y: x + y print(add(5,4)) print(add(2,3))In this example, 'add' is a lambda function that takes two arguments 'x' and 'y' and returns their sum. The function can be called like any other function, e.g. 'add(2, 3)' returns 5. Note that we didn't need to define a separate function using def, we simply used the lambda keyword to define the function inline.
Output
9 5 -
Add 25 to argument x, and return the result:
sum = lambda x : x + 25 print(sum(5)) # Output: 30Output
30In this example, 'sum' is a lambda function that takes one argument 'x' and returns its sum with 25.
-
Using Lambda function inside a function (nested function):
The power of lambda is better shown when you use them as an anonymous function inside another function.
Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number:
def func(n): return lambda a : a * n doubler = func(2) print(doubler(10))In the code provided, we define a function 'func(n)' that takes a single parameter n and returns a lambda function that multiplies its input by b. When we call 'func(2)', it creates a new lambda function that takes a single parameter a, and multiplies it by 2. This lambda function is then returned and assigned to the variable doubler.
Now, doubler is a reference to the lambda function returned by 'func(2)' ,which multiplies its input by 2. We can call 'doubler' with any input value, and it will return the result of multiplying that value by 2.
For example, when we call 'doubler(10)' ; it passes the value 10 as the input to the lambda function returned by 'func(2)', which multiplies it by 2. The result is 20, which is returned as the output of the function call:
Output
20 -
Python Lambda Function with if-else
# Example of lambda function to find max of two numbers Max = lambda a, b : a if(a > b) else b print(Max(5, 7))Output
7This code demonstrates the use of a lambda function to find the maximum of two numbers. The lambda function is defined with the syntax 'lambda a, b : a if(a > b) else b', which takes two input parameters 'a' and 'b' and returns the larger of the two values. We then call this lambda function by passing the values '5' and '7' as input using the syntax 'Max(5, 7)'. The lambda function evaluates the expression 'a if(a > b) else b', which returns the value 7 since a=5 is not greater than b = 7.
Finally, the output value of 7 is printed to the console using the 'print()' function. This demonstrates the simplicity and power of lambda functions for performing quick and efficient operations on input values.
Output
7
Advantage of using Lambda Functions
One of the main advantages of lambda functions is that they allow you to write more concise code. Instead of defining a separate function and giving it a name, you can define a lambda function inline, at the point where it is needed. This can make your code more readable and easier to understand, as you don't have to look for the definition of the function elsewhere in the code.
Some common scenarios when you might want to use lambda functions
-
As a callback function to another function: If you have a function that takes another function as an argument, you can use a lambda function to define a quick and simple callback function without having to write a separate function definition.
-
For concise code: In general, lambda functions can help make your code more concise and easier to read by eliminating the need for separate function definitions in some cases. However, be careful not to overuse them, as they can also make your code harder to understand if used excessively or inappropriately. The lambda function approach is shorter and more concise, as it eliminates the need for a separate function definition.
In summary, lambda functions provide a convenient and concise way to define small, one-time use functions, particularly in situations where a function is required as an argument to another function. They can make your code more readable and easier to understand, and can simplify the syntax of your code by eliminating the need for separate function definitions in some cases.
Library
WEB DEVELOPMENT
FAANG QUESTIONS

