In this series of python tutorial we are going to learn next python function i.e python lambda function. What are the syntax of python lambda or anonymous function. Where and How to use python anonymous/lambda function.
What is Lambda or Anonymous function in python?
The python function which is defined without name is knows as Lambda or Anonymous function.
Lambda function is not declared in standard manner like other python function types. Like, While defining a normal python function we use def keyword. Whereas for defining a lambda or Anonymous function we use lambda keyword. That is the reason lambda function is also called as the anonymous function.
Syntax of python Lambda/Anonymous function.
lambda arguments: expression
- One important thing about python lambda function which we should always keep in mind is that, Lambda function can have any number of arguments but only one expression which gets evaluated and returned.
# Python lambda sample program.
add = lambda x, y: x+y;
In above python lambda function x and y are the arguments and “x+y” is the expression.
Difference between normal (def) python function and lambda function:
To understand the basic difference between normal python def function and anonymous/lambda functions let’s see one sample python program.
This is the python program for calculating area of rectangle.
# Normal python function written using def
def area (length, width):
# Python lambda or anonymous function.
rect_area = lambda len, wid: len*wid
In above python program both the functions are written to calculate the rectangle area. Only difference is first function is defined using normal def keyword and the second function is a lambda function.
- Without lambda function: We needed to define function with name “area“. After the execution we needed to return the result from where the function was called.
- With lambda function: Lambda function do not require a name and return statement. It always contains an expression which gets evaluated and its value is returned. So no need of return statement. Lambda function is very simple to use.
Use of lambda function in python:
Lambda function is used when it requires a function for very shorter period of time. It can be used along with other built-in functions like filter(), map() and reduce().
Using python lambda function along with filter():
python filter() function takes function and list as an argument. To understand lets see one example. Here we’ll provide one numbers list as an argument and it will return us a new list with even numbers.
# Python program to create list of even numbers using lambda and filter function.
random_number_list = [1, 2, 3, 4, 5, 6, 7, 8]even_number_list = list(filter(lambda x: (x%2==0), random_number_list))
[2, 4, 6, 8]
In above example we are having a list named “random_number” from this list we have filtered out odd numbers and created new list of even numbers named “even_number_list”. To this we have used python filter() function along with lambda(). In lambda() function we have used expression “x%2==0” to find out the even number.
Using python lambda function along with map():
Python map() function takes a function and list as an argument. Here for learning purpose we are going to provide lambda function and a one existing list as an argument to the python map() function.
So let’s see the example how python lambda function can be used along with python map function.
# Sample program demoing use of python map function along with python lambda function.
original_list = [1, 2, 3, 4, 5, 6, 7, 8]New_List = list(map(lambda x: (x*3), original_list))
[3, 6, 9, 12, 15, 18, 21, 24]
In above python program we have are having a list of numbers (original_list) and now we want to create a new list with each original list item multiplied by “3”.
To do this we are going to use python map() function along with lambda(). As said earlier we are going to provide existing list (original_list) and python lambda function as an argument to the map() function. Inside lambda function we are giving the expression “x*3” so that it will multiply each original list item by “3” and return it to the map function. The map function will create a new list (new_list) of all the items returned by the lambda function.
Using python lambda function along with reduce() function:
Like above functions, This reduce() function also needs a list and a function as an argument. Let’s see one sample program to perform sum of all the list items using python reduce() and lambda function.
#Python prog. to do sum of all the list items using python reduce and lambda function.
from functools import reduce
original_list = [1, 2, 3, 4, 5, 6, 7, 8]New_List = reduce(lambda x, y: (x+y), original_list)
Since reduce function is a part of functools module so we have to import functools module first. Here we are giving “original_list” and “lambda” function as a input to the reduce() function.
Inside lambda function we have defined x, y as an argument and “x+y” as an expression. So the lambda function will add previous elements to the next element and this goes on till the end of the list like (((((((1+2)+3)+4)+5)+6)+7)+8).
- What is python recursive function?
- Learn how to define function in python?
- How to pass inputs/arguments to a python function?
Also you can get more help on python forum. Click Here