In this blog we would try to learn about Python lambda function, its working mechanism, and many more concepts has been given about lambda function, so let’s begin.
anonymous function can be called lambda function only when it can returns a function object and can be passed as an argument to another function. It is not necessary that the lambda function and the anonymous function are the same things in any other programming languages.
Actually Lambda function and anonymous function are the synonyms in Python. So there is no need to be confused between these two names.
Most of the popular programming languages contain the concept of
anonymous functions. They are also called
lambda expressions or statements in many programming languages.
Lambda functions are anonymous because it doesn’t have not any name. It is declared without name. You assign them to an object and they are used by the same object name. This eliminates the need to write a complete function using multiple keywords.
lambda <arguments> : expression
lambda keyword: In Python, We define lambda function by using the lambda keyword whereas for normal function declaration we use
arguments : After the Lambda keyword, we need to define the arguments. similar to normal functions, you can pass any number of arguments to lambda functions in python as per your wish. In below syntax argument_1, argument_2, argument_3, argument_4 and argument_n are arguments.
lambda argument_1, argument_2, argument_3, argument_4……argument_n : expression
expression : After the lambda keyword and arguments we need to write expression. The expression is defined by inserting a colon
: after the arguments. This expression is similar to the body of a normal function. but lambda function contains the single expression while normal function contains multiple expressions.
lambda argument_1, argument_2, argument_3, argument_4.....argument_n : expression
Only one expression can be defined in
Lambda functions. The syntax of
Lambda functions does not allow the use of multiple expressions. This is the reason why lambda functions are also called one-line functions.
There is no return statement in lambda functions while in normal function, there can be a return statement. In lambda function, after the evaluation of expression, its result is returned automatically.
An object is assigned by defining lambda functions. After that, we can access that lambda function using that object. We can also pass arguments in that object that’s why It is a function object.
lambda_fn_object = lambda argument_1, argument_2, argument_3,....argument_n : expression
In this way, you can use lambda functions wherever any function object is needed.
Lambda functions are single-use functions. That’s why you create them only where you need them. These are also called throwaway functions.
Whenever you need a single expression logic, you can create a lambda function instead of creating a complete function.
For example, if you want to assign a value to an object by first calculating it. In such a situation, you can evaluate that value by lambda function and after that, it can be assigned to the object.
Function as an argument
lambda is a function object. That’s why you can also pass
lambda function as an argument to another function. In such a situation, the value returned by the expression and held by the object will act as an argument.
lambda_fn_object = lambda x, y : x + y # creating a lambda function def example(lambda_fn_object): # passing lambda function object as argument print(lambda_fn_object(1, 2)) example(lambda_fn_object) # calling the function with lambda function object # Output : 3
In Python, lambda functions are mostly used as arguments to the filter(), map() and reduce() functions.
Lambda function with filter
The filter is a function in Python that is used to filter and extract data from a list on the basis of logic. In this function, you pass a function and a list as arguments.
Filter function accepts two arguments, one is a function object and the other is a list, tuple, etc. A function object is called for each element of the list or tuple and the filter returns only those elements for which function object is true.
Like the map function, the filter function also returns a map of an element. The filter function can have only one iterable(list, tuple, etc).
Logic is performed with all the items of the list, the item which corresponds to that logic is stored. Thus all those items that fulfill that logic are returned.
Syntax of filter()
For example, if you want to list all those list items which are even in the first 10 natural numbers then this can be done by lambda functions.
number_list = [1, 2, 3, 4, 6, 8, 9, 10 ,12] filtered_list = list(filter(lambda x : x%2==0, number_list)) print(filtered_list) # Output : [2, 4, 6, 8, 10, 12]
Here as you can feel lambda function would be a great fit instead of a normal function. Because you will not need to create a function separately. Wherever you need to, you define it on a single line.
Lambda function with map
The map is a function in Python that is used to map data based on logic. In this function, you pass a function and a list as arguments. Map function accepts a function object, list, dictionary. It implements a function object for each element in the sequence and returns a map of the element modified by the function object.
Syntax of the map()
For example, if you want to print a multiplication table of any number then this can be done by lambda functions. We are going to map the multiplication table of 7.
number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] multiplication_table = list(map(lambda x: x*7 , number_list)) print(multiplication_table) # Output: [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
Lambda with reduce function
Reduce function is used to do the reduction or folding of a list of items into a single value. This function belongs to
Syntax of reduce()
For example, if you want to print the sum of the first 10 natural numbers then this can be done by lambda functions.
import functools number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sum_output = functools.reduce(lambda x, y: x + y , number_list) print(sum_output) # Output : 55
In the above information, we read about
lambda function which is an anonymous function. Also gone through its syntax. then we learned how to pass lambda objects in another function.
We also saw examples of
lambda function using