Lambda in Python
Lambda functions in Python are anonymous functions that are defined without a name. They are typically used to create short, throwaway functions that are not needed elsewhere in the code. Lambda functions are useful when a function is required for a short period of time, and the function is not going to be used elsewhere in the code. They can also be used as an argument to higher-order functions like filter(), map(), and reduce().
Lambda functions are defined using the “lambda” keyword followed by the argument list, followed by a colon and the expression to be evaluated. The result of the expression is the return value of the function.
An anonymous function in Python is a function that has no name. As we previously know, the lambda keyword is used to construct anonymous functions, whereas the def keyword is used to define regular functions. You can check this also: Lambda Expressions.
lambda arguments : expression
The syntax for defining a lambda function is quite simple. It begins with the lambda keyword, followed by any number of parameters, separated by commas, and then a colon. After the colon, you provide the expression that the function should evaluate, which is then returned as the function’s result.
Only one expression is evaluated and returned in this function, regardless of the number of parameters. Lambda functions can be used anywhere that function objects are required. The fact that lambda functions are syntactically limited to a single expression must always be kept in mind. In addition to other kinds of expressions in functions, it has a variety of purposes in certain areas of programming.
Example:
value = 'Python'
print(lambda string: value)
Output:
<function <lambda> at 0x7f9d080c6dc0>
The print function does not call the lambda in this example; instead, it simply returns the function object and the memory location where it is stored. So, in order for the string to pass the print, we must first call the lambda.
Example:
value = "Python"
(lambda x: print(value))(value)
Output:
Python
The distinction between lambda and regular function calls. Lambda functions are a powerful tool in Python that can help you write more concise and efficient code. However, they should be used sparingly and only for small, one-time operations, as they can make code harder to read and understand if used excessively.
Example:
def triplet(y):
return y*y*y
def another_triplet(x): return x*x*x
print(another_triplet( 10 ))
print(triplet( 10 ))
Output:
1000
1000
The comparison between lambda function calls and ordinary function calls. The power of lambda is best demonstrated when used as an anonymous function inside another function. Assume you have a function definition that takes one argument and multiplies it by an unknown number:
Example:
def exponent(number):
return lambda i: i ** number
base = exponent(3)
print("4 power of 3 = ", base(8))
base = exponent(5)
print("4 power of 5 = ", base(8))
Output:
4 power of 3 = 512
4 power of 5 = 32768
Example:
numbers = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
filtered_list = filter (lambda x: x % 2 == 0, numbers)
print(list(filtered_list))
mapped_list = map (lambda x: x % 2 == 0, numbers)
print(list(mapped_list))
Output:
[22, 24, 26, 28, 30]
[False, True, False, True, False, True, False, True, False, True]
We can also use a map() function to replace list comprehension with Lambda, which is not only fast but also efficient. Let’s also look at how to use lambda in the filter ().
Python’s filter() function accepts a function and a list as inputs. The function is called with all of the items in the list, and a new list is returned with the elements for which the function returns True. Here’s an example of how to use the filter() method to choose only even numbers from a list.
Python’s map() method accepts a function and a list. The function is called with all of the list’s items, and a new list is returned with items returned by that function for each item. Here’s an example of how to use the map() method to double all of the entries in a list.