Python Lambda Function

    In this tutorial, we will discuss one of the most important Python concepts known as the Python Lambda function aka anonymous function. We will also know how and where to use it. So let us get started!

    Python Lambda Function

    What are lambda functions in Python?

    In Python, there is a keyword known as lambda which is used to create a user-defined function without defining the function name or any def keyword. Python lambda can create a user-defined function without the function name that’s why they also known as an anonymous function.

    Normal Function Vs lambda function:

    Normal Functions Lambda functions
    To define a Normal function, we use def keyword. To create an anonymous function, we use the lambda keyword.
    We write a normal function in multiple lines The lambda function can be written in one single line
    To return a value we write to return keyword in the normal function We do not write return keyword in lambda
    A normal function can have multiple expression A lambda function can have only one expression
    Example:
    def cube(x):
        return x**3
    
    cube(3)

    #Output

    27

    cube = lambda x: x**3
    cube(3)

    #Output

    27

    How to create and use a lambda function in python:

    Unlike write code in different lines to create a function using a def keyword with lambda function you can create a function in one line.

    Syntax of the lambda function

    lambda arguments: expression

    in the above syntax, the lambda is the main keyword which we define in every lambda function and the argument are similar to the normal argument used in def functions. The expression part should be the logic because the lambda function return the expression, which means it returns the value which we write after the semicolon (:). The question is if we are creating a function without function name so how to call it? To call a function created with lambda we need to assign a name to that lambda function.

    Syntax of assign a name to lambda function

    name = lambda agruments : expression               #now name has become the function.

    Let’s understand lambda function with an example:

    Example 1

    cube = lambda argument : argument * argument * argument
    print(cube(3))

    #Output

    27

    Example 2: The same function using def

    def cube(argument):
        return argument*argument*argument
    
    print(cube(3))

    #Output

    27

    Behind the code

    In example 1 we have written a lambda function which takes an argument name argument and return the value argument* argument* argument. we also have assigned a name cube to the lambda function so with the help of the name we can call it. Though often we do not assign a name to the lambda function, we directly use it with python in-built functions such as map() and filter().

    Python filter() function

    filter() is an in-built function present in python which accepts 2 values as arguments the 1 value should be a function and the second value is iterable. The filter() pass the iterable values one by one to the function and return only those values which satisfy the function.

    Syntax of filter function:

    filter(function, iterable) 

    Let’s understand it with an example:

    lis = [1,3,5, 4, 7, 6]
    filtered= list (filter (lambda x : x%2==0 , lis))
    print(filtered)

    #output

    [4, 6]

    Python map() function:

    map() function is similar to the filter() function which accepts 2 arguments one is function and other is iterable the only difference is the map() function apply the function condition on every value of iterable and doe not omit any value, whereas the filter() function omit the iterable values which do not suit the function condition.

    Syntax of map() function.

    map(function, iterable)

    Let’s understand it with an example:

    lis = [1,2,4,5,6]
    lis_sq = list(map (lambda x:x*x,lis))
    print(lis_sq)

    #Output

    [1, 4, 16, 25, 36]