Lambda and Anonymous Function in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

Lambda Function

Overview

A function defined in Python without a name is known as an anonymous function. In Python, the def keyword defines regular functions, whereas the lambda keyword defines anonymous functions. As a result, lambda functions are another name for anonymous functions.

Scope

  • In this article, we will start by learning the syntax of the Lambda Function in Python.
  • We will see why we use the lambda function instead of the regular function?
  • We will also learn about using lambda functions inside different functions in Python.

Introduction

So often, we want a short and elegant anonymous function that will take up the memory for a shorter period, for all these python lambda functions come in very handy. With the lambda function and anonymous function in python, so many complex tasks feel like a cakewalk.

The Python lambda function sounds like a jargon word, but it is not jargon. It is one of the most powerful functions in Python.

It gives so much power to the programmer so that they can define functions anywhere without giving them a name, and that’s why the lambda function is also called an anonymous function.

In normal functions, we use the keyword def to define the function, then we give it a name and write the block of code inside it. But now, consider a situation where you want to evaluate only one expression, then this lambda function in python is convenient.

The Syntax of the Lambda Function in Python

Python Lambda Anonymous Function It is possible to use the lambda function only once and execute it simultaneously when it is defined, and due to this property, it is also called an anonymous function.

This anonymous function is beneficial when we want to pass functions as an argument to the other functions.

Output:

The above example has a single argument x, and we have written the entire lambda function inside a print statement.

Here, we passed 4 as a parameter as soon as we defined the lambda function.

The above code will print the value 10, which is nothing but the addition of 6 and 4.

In the lambda function, we can pass multiple arguments but only a single expression, so obviously, it can’t be replaced with the regular function, which has loops and conditionals in its definition.

Let’s compare python lambda function with the regular function,

Output:

As Python treats everything as an object, we assigned the lambda function to the variable sum_lambda, and then we used it directly in the print statement to do the addition.

We can call the lambda function a regular function as we did in the above example, but lambda is more than that.

In lambda functions, we don’t write the function's name as well as there is no return keyword.

So why do we use the lambda function instead of the regular function? The actual power of the lambda functions can be utilized when they are used with some other in-built functions like filter(), map(), or reduce().

Lambda functions are passed as anonymous functions to evaluate the answer and return the results so that you can write clean and easy-to-understand code.

1. Use of the Python Lambda Function Inside the filter() Function

As the name suggests, the filter() function in Python will help us to filter the lists.

The filter function takes two arguments: the first is the lambda function as an anonymous function, and the second argument is the list of numbers.

Syntax: filter(function, list)

When we call the filter function, it iterates over the provided list and passes every value one by one to the function, which returns true or false.

If that particular value's result is true, it gets added to the list. The classic example of lambda functions with the filter() method is to filter out the even or odd numbers from a list of numbers.

Output:

In the above example,

  • We passed the lambda function as an anonymous function as the first parameter and the list of numbers as the second parameter.
  • The numbers from a list get passed as a parameter as we iterate through the list and get evaluated with the expression inside the lambda function which is used as an anonymous function.
  • Every time an expression lambda function is evaluated as true, a new value gets appended to the new list of even numbers, i.e. even_no.

2. Use of the Python Lambda Function Inside the map() Function

The Python map() function is mostly used to modify the items in the list.

It takes two arguments, the first argument is the lambda function as an anonymous function, and the second argument is the list.

Syntax : map( function, list)

The map() function iterates over the list provided as a parameter and each value is passed as a parameter to evaluate with the expression of the lambda function. Then these evaluated values get returned one after another.

Suppose we want to create a list of squares of numbers from the given list of numbers.

Output:

In the above example,

  • we passed the list of numbers as one of the parameters.
  • The map() function will iterate over this list. Every number is passed as a parameter to our anonymous function.
  • The map() function will return the evaluated values from the lambda function

3. Use of the Python Lambda Function Inside reduce() Function

The Python reduce() function is mostly used to reduce the entire list into a single result.

The reduce() function needs to be imported from the functools module. Functools is one of the modules in python which contains some higher-order functions.

The reduce() function takes the two arguments: the first is the function, and the second is the list.

Syntax : reduce( function, list)

The reduce() function iterates over the given list of numbers, and these numbers are passed to the lambda function as a parameter. When the entire list is iterated, the final result is returned by the reduce() function.

Let’s suppose we have the list of numbers and do the addition of all these numbers.

Output:

In the above example,

  • The reduce() function iterates over the list of numbers and evaluates them one by one.
  • With the expression in the lambda function, which we passed as an anonymous function, the final result is returned.
  • Here, the addition which we have done is similar to this (((((1+2)+3)+4)+5)+6).

Conclusion

  1. The lambda function in Python can be used at various places and their powers are being utilized to write clean and simple code.
  2. This python lambda function gives a very elegant way to do complex things easily.
  3. The power of the lambda functions is utilized when they are used with some other functions like filter(), map() and reduce(), etc.

Read More: