In python, lambda  is a function that is defined without a name it is also called anonymous function. lambda function are defined using lambda keyword while normal functions are defined using def keyword. lambda functions are very short functions just not more than a line and  you can pass any number of arguments just like in normal functions.

Syntax of lambda function:

lambda arguments : expression

it only use one expression and can have any numbers of arguments. The expression is evaluated and returned. whenever function object are required then you can use lambda function. There is no need for any return statement in lambda function.

Let's look at the example and try to understand the difference between normal function and lambda function in python

Normal function in python :

Example:

def multiply(a,b):
    return a*b

In the above example, we define one function i.e multiply and pass two arguments . it will  return the multiple of a and b.

lambda function in python:

Example:

 x = lambda a,b: a*b
 
 x(2,3) #call the lambda function
 

Here we are using two arguments a  and b , expression after colon is the body of the lambda function. As you can see lambda function has no name and is called through the variable it is assigned to.

In python, lambda function is generally used as an argument to an higher order function i.e a function that takes in other function arguments.  Lambda functions are used along with built-in functions like filter(), map() etc.

Filter

filter is used to filter an iterable  element. The filter function takes two arguments, one is a function and second is iterable. The filter function will filter out every element of the iterable for which the function passed to it is false. And we are left with only elements for which the function is true.

Syntax:

filter(function, iterable)

Example:


a = range(1,11)

def even(n):
    if n%2==0:
        return True
    return False

print (list(filter(even,a)))

In the above example, we are taking list of number from 1 to 10 and use filter function to find even numbers.  We will pass a function which returns True if the number is even and the list to the filter function.

Instead of using regular function we can also use lambda function to pass to the filter function.

a = range(1,11)
print (list(filter(lambda x:x%2==0,a)))

Output:


2, 4 , 6 , 10

Map

map function is similar to filter function. It also takes a function and iterables. We can pass multiple iterables to the map function.

Syntax:

map(function, iterable1,iterable2 ...)

‘map’ function implements the function passed to it to every element of the iterable.

Example:

def multiply(n):
    return 2*n

Every element of the array will be multiplied by 2. In the case of multiple iterables, the passed function must take those many arguments to it. For example, if we have passed two iterables ‘a’ (a = [1,2,3]) and ‘b’ (b = [4,5,6]), and a function to add two arguments passed to it:

a = [1,2,3]
b = [4,5,6]
def ad(x, y):
    return x+y

Then each element of the list ‘a’ will get added to the corresponding element of the list ‘b’. For example, 1 will be added to 4, 2 will be added to 5 and 3 will get added to 6, thus giving us [5,7,9]

Example:

 a = range(1,11)
print (list(map(lambda x:x*2,a)))

Output:

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]