Anonymous Functions in Python

Anonymous functions are also called lambda functions in Python because instead of declaring them with the standard def keyword, lambda keyword is used.

Anonymous functions are used when there is a requirement of a nameless function for a short period of time. These functions are created at runtime. Depending upon the program you need, lambda function works in conjunction with filter(), map() and reduce():

The filter() function filters, as the name suggests, the original input on the basis of the conditions given. With map(), you apply a function to all items of the list. The reduce() function is part of the functools library. You use this function cumulatively to the items of the list, from left to right and reduce the sequence to a single value. Let’s see how to use them.

The function sq(n) is having a parameter n which will return a value of n multiplied with 3. For example, when n = 3, the output is 6. The same can be written using the lambda function.

We will use the lambda keyword. The parameter is written first. Then a colon sign is placed. Then the operation is written. Let’s see one more example.

The program is simple. It’s checking that in between two numbers, which number is bigger. There are two parameters, m and n. While writing with lambda we will write lambda m,n. Then, we have the body part. It will return the result m if m>n, else it will return the value n. Let’s write it now using the lambda function.

I have a list of numbers, and the task is to generate another list which will contain the squared value of the previous list. There will be a list. Then, an empty list will be generated. We will check every value of our list. We will square every element and then will append every value in the empty list. Then we will return the empty list. Let’s code it down first using a function. 

With lambda we can write it like this. We have x and this variable has to be squared, that is, x**2. But from where it will take these values of x. From the list l. Now we will need to map our squared result with our list l. For this, the  map function is used. This function is then passed through the list function.

Let’s take the above list and do another operation. We have to make a list of elements which are greater than two. We only have to check that if x>2, then we will append this value of x, in an empty list. Then finally, return this empty list.

Technically, we are filtering our data. We are filtering those values of x, which are greater than 2. 

We will write a filter command instead of map command. We can also use the map command. It will print the result in Boolean.

3 and 4 are greater than 2, hence, a True Boolean value is displayed. For 1 and 2, False Boolean value is printed. Let’s do one more task. I need to multiply each value of my list and print the result. In the above case, my list has values 1,2,3,4. The result will be 1x2x3x4 = 24.  

In this case, we will need to reduce our list to a single value. Here we can use the reduce function. As stated earlier, reduce function comes in the functools package. For multiplication, we need two values, x and y. Let’s see how to write it.

That’s all for Anonymous Function.

Leave a comment

Design a site like this with WordPress.com
Get started