Python

Python lambda Function Examples

A lambda function is a function with no name, mostly known as the ‘Anonymous’ function. Mostly written and performed in a single line. In any normal function in programming, we utilize ‘def’ to define a function. Likewise, we use lambda to define any anonymous function. In this article, we have covered various categories of lambda functions to elaborate on all associated programs.

Prerequisite

To understand the functionality of lambda, you need to install the Spyder tool on your system with python extensions. If you want involvement of the Linux operating system, then you need to install Ubuntu on a virtual box.

Syntax

lambda arguments: expression

The function may contain more than one argument but it has only one expression.

Lambda with One Argument

Example 1:

In this example, the arithmetic operator of multiplication is used. We have taken a vairiable that will store a value named ’double’. Then, lambda function is used in such a way that x is the argument here and after the colon, the expression part is started in which the value of x is multiplied with a constant number. In the next line, we will print the value and we will pass a value in the variable which is to be altered.

Now, to check the output go to the Ubuntu terminal and run the following command:

$ python3.8   ‘/home/aqsa/sample.py’

Here, python keyword is used as we are using the python language in the respective tool. Whereas you can use the file name following the keyword, here, I have used the file path. File must be saved with the extension of .py that relates to python.

Example 2:

This is another example with some different methods. One variable stores the value performed after the lambda function, which is the square method. Again, a single argument is used here. Now in the second line, the value is given on which we have applied the operation. ‘number’ is the new variable that stores the output value. Now, take the print of the result.

Again for the output, go to the terminal and view the result. The result shows the square of the number 9 that is 81.

Lambda with Two Arguments

In this example, we have taken two arguments to perform the function on them. The subtraction function is performed. The word subtraction contains the value obtained from the function. Two values are provided by passing as a parameter.

Check the output using the given command.

Filter() Function in lambda

A filter function is used to select some specific words/elements from the series of elements. The filter function’s output mostly contains elements fewer than the original data. The elements are selected according to some given condition. This function contains two parameters. These are as follows:

  1. The function in which the filtration process is defined.
  2. The second one is the sequence that might be a list or other series.

Let’s have a look at the example. There is a list of numbers present in the variable named as a number. The filter function we are going to provide is the ‘greater than’ operator to bring the numbers that are greater than a constant provided that is 18. ‘Greater’ variable will store the answer. The filter function syntax is as follow:

Filter (object, iterable )

Variable2 = list(filter(lambda x : x > number , variable1))

The words ‘list’ , ‘filter’, and ‘lambda’ will be constant for each filter function. List will take the value and filter will perform the function. In the end the values are printed.

The output is given below. It contains all those numbers that are greater than 18.

Map () Function in lambda

Example 1:

The purpose of map function() is to map the input corresponding element in the output list. It also contains the two parameters just like filter function. But the difference in the filter and map function is that in filter the output may contain fewer elements than the input elements, and map contains more than one iterable object. Syntax of the map function is:

Map(object, iterable1 , iterable2, …..)

Now, let’s have a look at an example. We have taken a variable to have all integer values. We want to take a square of the value. The function methodology is quite the same as the filter methods.

In the output, you can observe each number’s square is present without any filtration.

Example 2:

Now, moving towards our next example. It is related to the one in the filter function. We want to find the greater number than 18 present in the list of numbers. As discussed earlier that in the map function, each value in input is mapped with output. In easier words, number of inputs is similar as the number of output. So, instead of giving the output with numbers, we come with a boolean answer. In which the output shows ‘true’ or ‘false’. If the condition is applicable means the number is greater than the constant ‘18’ then it’s true else it is false.

From the Ubuntu terminal, you can see the output.

As described above, 13 is smaller than 18 so, in this case, the value is false and so on.

Example 3:

In this example, we will provide fruit’s name in lowercase letters and will get all the characters in the capital letters. The function that converts the case of the fruit is given below. Otherwise, the whole functionality is the same.

$ Str.upper(fruits)

The output is as follows

Reduce () Function in lambda

The reduce function is used to reduce the number of outputs as compared to the input by performing different functions. To perform this function we need to introduce functools, as to reduce belongs to it. Steps that are used in reduce function are as follow:

  1. The defined operation is performed on the first two items in the list or dictionary etc.
  2. Result is saved.
  3. Operation is performed with the resultant element and the next element.
  4. Repeat the above process with all elements until no element is left behind.

Moving towards the example, firstly we have to import the reduce from functools. Then a variable is introduced having integers. Sum operation is applied to all the numbers.

$ New = reduce(lambda x,y : x+ y, num)

The output is given below.

Conclusion

In this guide, we have described the foremost functionalities of the lambda function. These examples will surely help you in gaining information regarding this function.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.