Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - LAMDA / ANONYMOUS FUNCTION


A 'Lambda' or 'Anonymous Function' is a small one liner function that can just have only one expression.


Let us write the 'Function' that adds two numbers and returns the result.


def add(first_number, second_number):
    result = first_number + second_number
    return result

Now, let us write, exactly same 'Lambda' or 'Anonymous Function' for the above 'add' Function.


add = lambda first_number, second_number : first_number + second_number

And we are done.


java_Collections

Note : Just note that a Lambda Function can only have one expression.

Let us see the below example with to add two numbers using 'Lambda' or 'Anonymous Function'.


Example :


add = lambda first_number, second_number : first_number + second_number

first_num = 5
second_num = 4   	
value = add(first_num, second_num)
print("The added result is ",value)


Output :



  The added result is 9

So, in the above code the execution of the program starts from the second line. As 'add' is a 'Function'(i.e. A Lambda)


java_Collections

So, we have the variables, 'first_num' and 'second_num' for addition of two numbers.


first_num = 5
second_num = 4

java_Collections

And in the next line, we call the 'Lambda' or 'Anonymous Function'.


value = add(first_num, second_num)

The 'Lambda' or 'Anonymous Function' call is just like any other Function call. And the 'Lambda' Function call is made.


add = lambda first_number, second_number : first_number + second_number

And the 'Lambda' or 'Anonymous Function' is executed, adding two numbers and returning the added result.


We have explained the details of 'Lambda' or 'Anonymous Function'.


And the added result is printed.


The added result is 9

What is the use of Lambda or Anonymous Function?


A 'Lambda' Function can be used in,


  1. A Function Argument
  2. Or can be used in the return statement of a Function.

At First let us see, how a 'Lambda' Function can be used in a Function Argument.


Use of Lambda Function in a Function Argument


Example :


x = [(4, 'Cal'), (3, 'Ali'), (2, 'Daniel'), (1, 'Bose')]
x.sort(key=lambda x: x[1])
print(x)


Output :



  [(3, 'Ali'), (1, 'Bose'), (4, 'Cal'), (2, 'Daniel')]

So, in the above example, we have used 'Lambda' in the argument of 'sort( )' Function.


So, we have created a List with four tuples inside it.


java_Collections

Then we have used the 'sort( )' Function to sort the List.


x.sort(key=lambda x: x[1])

And put the 'Lambda' Function as a value to the 'key' (i.e. 'key=lambda x: x[1]'), such that the sorting happens with the second element of the Tuple (i.e. 'Cal', 'Ali', 'Daniel' and 'Bose') inside the List.


java_Collections

And the print statement,


print(x)

Prints the sorted List.


Output :



  [(3, 'Ali'), (1, 'Bose'), (4, 'Cal'), (2, 'Daniel')]

Use of Lambda Function in the return statement of a Function


To understand the use of 'Lambda' Function in return statement. Let us try to corelate with another example.


Let us, say, we have a first Function, whose return statement calls another Function.


Let us make it a little simpler with the below example.


Example :


def second_fun(p, q):
    return p*q

def first_fun(n):
    return second_fun(5, n)
	
i = first_fun(2)	
print(i)


Output :



  10

So, in the above example we have two Functions,


  1. def first_fun(n):
  2. def second_fun(p, q):

So, at first, we have called the first Function 'def first_fun(n):' passing the value '2' as argument.


i = first_fun(2)

And the execution of the Function 'def first_fun(n):' begins,


def first_fun(n):
    return second_fun(5, n)

Now, the value of 'n' is '2' as '2' is passed as an argument.


java_Collections

Then in the 'return' statement, we have done something different,


return second_fun(5, n)

We have called the second Function 'def second_fun(p, q):' from the return statement.


So, the value won't be returned. And will be waiting until the execution of second Function 'def second_fun(p, q):' is over.


java_Collections

And the control goes to the second Function 'def second_fun(p, q):' and its execution starts.


def second_fun(p, q):
    return p*q

And the first argument (i.e. 5) and the value of 'n' (i.e. '2') is passed to the second Function as argument.


java_Collections

And the value of 'p' and 'q' is ''


java_Collections

Then in the next line, we 'return' values of 'p' and 'q', multiplying them(i.e. 'p*q').


return p*q

So the returned value of the second Function, 'def second_fun(p, q):' is 'return p*q' i.e. '10'.


second_fun(p, q) --- 10

Now, the control comes to the return statement of the First Function, 'def first_fun(n):' that was unfinished.


java_Collections

And substitute the value of 'second_fun(x, n)' with '10'(Since that is the return value of the second Function).


java_Collections

And the returned value of the the First Function, 'def first_fun(n):' is '2'.


i = first_fun(2)

Which gets assigned to 'i'. And the print statement.


print(i)

Prints the value of 'i'.


Output :



  10

Now, let us rewrite the same example using 'Lambda' Function.


Example :


def first_fun(n):
    return lambda p : p * n
	
i = first_fun(2)	
print(i(5))


Output :



  10

So, in the above example, we have replaced the entire second Function,


def second_fun(p, q):
    return p*q

With the Lambda Function in the return statement,


lambda p : p * n

Now, if we see the above example,


java_Collections

The call to the first Function 'def first_fun(n):' is made,


i = first_fun(2)

And the first Function 'def first_fun(n):' gets executed,


def first_fun(n):
    return lambda p : p * n	

And in the return statement, 'Lambda' Function does a lot in a single line.


return lambda p : p * n

  1. It takes the argument '2' and passes it to the variable 'p'.

    java_Collections
  2. Then it calculates evaluates the expresion

    p * n


    Although, the value of 'p' is '2' but the value of 'n' is unknown to us.

    And incomplete statement is stored in 'i' as the return value of 'first_fun(2)' (i.e. 'p*n' or 2*n).

    i = first_fun(2)

Now, just remember, 'i' is a function that represents Lambda Function.


java_Collections

Next, we make a call to the 'Lambda' Function using 'i' passing the value of 'n' in the argument of 'i'.


i(5)

And the value of 'n' is substituted with '5', i(5) = 2*5 = 10.


java_Collections

And the print statement,


print(i(5))

Prints '10' as output,


Output :



  10