How to Use a Lambda Function in Python Programming Language

Posted on


This article is containing subject and material about Lambda function. The terminology of lambda function exist in python programming language. The following content will contain the information about what is a lambda function. Furthermore, it will also explain the pattern of the lambda function. Last but not least, it will also show the usage of the lambda function with some examples along with.


What is a Lambda Function

Lambda function is a function which does not have a name. In other words, Lambda function is an anonymous function. So, is it necessary to know and to understand about how to use a lambda function ?. Apparently the answer is relative depends on the circumstances. One of the main reason for using lambda function is if there is a need for quick purpose usage especially in order to pass an argument to a high order function. So, what is a high order function ?. A high order function is a function which is receiving the value in the form of a function.

What is the difference between normal function with a lambda function.

Lambda function basically is a simple function. It is a simple function because the function can only have one expression although it can has many arguments. In order to see the difference between normal function with a lambda function, below is the definition of both those functions :


Normal Function

The following is the definition of a sum operation to add two numbers in the form of a function. The function name is ‘add_function’ with two parameters or arguments. Those are  ‘a’ and ‘b’. The definition exist below :

def add_function(a,b):
    return a+b

So, the execution of the above function exist below :

>>> def add_function(a,b):
...     return a+b
>>> b = add_function(5,6)
>>> print(b)

Lambda Function

On the other hand, Lambda function has the following pattern for definition :

lambda argument : expression

So, how is the above definition into a lambda function ? Below is the definition of the above normal function performing the sum operation of two numbers :

lambda a,b : a+b

An example for executing the lambda function exist as follows :

>>> lambda a,b:a+b
<function <lambda> at 0x00000209F23E7790>
>>> print(lambda a,b:print(a+b))
<function <lambda> at 0x00000209F23E7550>
>>> c = lambda a,b:print(a+b)
>>> print(c)
<function <lambda> at 0x00000209F23E7550>

So, how can a lambda function useful in this kind of situation ?. Because there is no place for passing the argument upon calling the function. The solution is quite simple, just pass it to another variable. Later on, pass the value to that variable. Just execute the following sequence of commands :

>>> c = lambda a,b:print(a+b)
>>> c(10,6)

Comparing Normal Function with Lambda Function

There are several differentiation that set apart the normal function from the lambda function. Those differentiation are in the following list :

1. The lambda function can have many arguments but only one expression. The above definition of the lambda function only consist of one expression. That expression is ‘print(a+b)’. It exist in the end of the function definition. In case of the arguments or parameter, there are two of them. Those are ‘a’ and ‘b’.

2. Lambda function does not have any name. In the example above, a normal function has a name with the name of ‘add_function()’. In case of lambda function, there is no function name.

3. Lambda function can return an object function. In case of the lambda function, there is a return command of object where the location is in the expression of the lambda function definition. The following is just a command for printing the lambda function :

>>> print(lambda a,b:a+b)
 <function <lambda> at 0x00000209F23E7550>
>>> lambda a,b:a*b
 <function <lambda> at 0x00000209F23E7550>

The above example is showing that printing an actual definition of a lambda function will show the above output. On the other hand, in the previous example, the result of the operation available in the expression will returned an object. The operation above is a sum of two integer values. In the above example, the lambda function return an object of number as a result of the sum operation.


Passing Lambda Function to a High Order Function

The following is the real example where it returns an object function. This is another set of example for using a lambda function. It happens when passing an argument to a high order function. The high order function received an argument in the form of function. The purpose is to print the operation of two values as follows :

def print_sum_operation (object_function, a, b):
    print(‘Sum Operation from object_function is {0} ‘.format(object_function(a,b))

So, the high order function in this context is the function with the name of ‘print_sum_operation’. There are two parameter or arguments which is a and b. So, where is the lambda funciton actually ?. We are going to call the high order function and then pass the lambda function to the high order function along with the parameter. The function is simple as follows :

print_sum_operation(lambda a,b:a+b,3,4)

So, the execution of the above example exist as follows :

>>> def print_sum_operation(another_function,a,b):
... print('Sum Operation from object_function is {0}'.format(another_function(a,b)))
>>> print_sum_operation(lambda a,b:a+b, 3, 4)
Sum Operation from object_function is 7

Leave a Reply