Home » Python Lambda

Python Lambda Functions

In this tutorial, we’ll study anonymous functions, commonly called lambda functions. We’ll understand what they are, how to execute them, and their syntax.

What are Lambda Functions in Python?

Lambda Functions in Python are anonymous functions, implying they don’t have a name. The def keyword is needed to create a typical function in Python, as we already know. We can also use the lambda keyword in Python to define an unnamed function.

Syntax of Python Lambda Function

This function accepts any count of inputs but only evaluates and returns one expression.

Lambda functions can be used whenever function arguments are necessary. In addition to other forms of formulations in functions, it has a variety of applications in certain coding domains. It’s important to remember that according to syntax, lambda functions are limited to a single statement.

Example of Lambda Function in Python

An example of a lambda function that adds 4 to the input number is shown below.

Code

Output:

10  

The lambda function is “lambda num: num+4” in the given programme. The parameter is num, and the computed and returned equation is num * 4.

There is no label for this function. It generates a function object associated with the “add” identifier. We can now refer to it as a standard function. The lambda statement, “lambda num: num+4”, is nearly the same as:

Code

Output:

10  

What’s the Distinction Between Lambda and Def Functions?

Let’s glance at this instance to see how a conventional def defined function differs from a function defined using the lambda keyword. This program calculates the reciprocal of a given number:

Code

Output:

Def keyword:  0.16666666666666666  Lambda keyword:  0.16666666666666666  

The reciprocal() and lambda_reciprocal() functions act similarly and as expected in the preceding scenario. Let’s take a closer look at the sample above:

Both of these yield the reciprocal of a given number without employing Lambda. However, we wanted to declare a function with the name reciprocal and send a number to it while executing def. We were also required to use the return keyword to provide the output from wherever the function was invoked after being executed.

Using Lambda: Instead of a “return” statement, Lambda definitions always include a statement given at output. The beauty of lambda functions is their convenience. We need not allocate a lambda expression to a variable because we can put it at any place a function is requested.

Using Lambda Function with filter()

The filter() method accepts two arguments in Python: a function and an iterable such as a list.

The function is called for every item of the list, and a new iterable or list is returned that holds just those elements that returned True when supplied to the function.

Here’s a simple illustration of using the filter() method to return only odd numbers from a list.

Code

Output:

[55, 75, 13, 63]  

Using Lambda Function with map()

A method and a list are passed to Python’s map() function.

The function is executed for all of the elements within the list, and a new list is produced with elements generated by the given function for every item.

The map() method is used to square all the entries in a list in this example.

Code

Output:

[4, 16, 25, 1, 9, 49, 64, 81, 100]  

Using Lambda Function with List Comprehension

We’ll apply the lambda function combined with list comprehension and lambda keyword with a for loop in this instance. We’ll attempt to print the square of numbers in the range 0 to 11.

Code

Output:

0 1 4 9 16 25 36 49 64 81 100   

Using Lambda Function with if-else

We will use the lambda function with the if-else block.

Code

Output:

35  

Using Lambda with Multiple Statements

Multiple expressions are not allowed in lambda functions, but we can construct 2 lambda functions or more and afterward call the second lambda expression as an argument to the first. Let’s use lambda to discover the third maximum element.

Code

Output:

[6, 54, 5]  

Next TopicPython Files Io

You may also like