Python – Lambda Function

You’ll learn about anonymous functions, commonly known as lambda functions, in this article. You’ll learn what they are, how to utilize them, and what their syntax is (with examples).

What are lambda functions in Python?

Python Lambda Functions are anonymous functions, which implies they don’t have a name. The def keyword is used to define a typical function in Python, as we already know. In Python, the lambda keyword is also used to declare an anonymous function.

Python Lambda Function Syntax:

lambda arguments: expression
  • This function accepts any number of inputs but only evaluates and returns one expression.
  • Lambda functions can be used whenever function objects are necessary.
  • It’s important to remember that lambda functions are syntactically limited to a single expression.
  • Apart from other forms of expressions in functions, it has a variety of applications in certain domains of programming.

Example of Lambda Function in python

An example of a lambda function that doubles the input value is shown below.

# Program to show the use of lambda functions
double = lambda x: x * 2

print(double(5))

Output

10

The lambda function is lambda x: x * 2 in the given program. The parameter is x, and the expression that is evaluated and returned is x * 2.

There is no name for this function. It returns a function object that is associated with the double identifier. We can now refer to it as a standard function. The assertion

double = lambda x: x * 2

is nearly the same as:

def double(x):
   return x * 2

Use of Lambda Function in python

When we need a nameless function for a brief period of time, we employ lambda functions.

We usually utilize it as an argument to a higher-order function (one that accepts other functions as arguments) in Python. Lambda functions are used in conjunction with built-in methods such as filter() and map().

Example use with filter()

In Python, the filter() method accepts two arguments: a function and a list.

The function is called with all of the items in the list, and a new list is returned that contains just those elements for which the function returns True.

Here’s an example of how the filter() function may be used to remove only even numbers from a list.

# Program to filter out only the even items from a list
my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(filter(lambda x: (x%2 == 0) , my_list))

print(new_list)

Output

[4, 6, 8, 12]

Example use with map()

A function and a list are passed to the map() function in Python.

With all of the items in the list, the function is called, and a new list is returned with elements returned by that function for each item.

Here is an example use of map() function to double all the items in a list.

# Program to double each item in a list using map()

my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(map(lambda x: x * 2 , my_list))

print(new_list)

Output

[2, 10, 8, 12, 16, 22, 6, 24]

Example use with reduce()

In Python, the reduce() method accepts two arguments: a function and a list. A lambda function and an iterable are used to invoke the function, and a new reduced result is returned. This performs a repeating action on the iterable’s pairs. The functools module contains the reduce() method.

# Python code to illustrate
# reduce() with lambda()
# to get sum of a list
 
from functools import reduce
li = [5, 8, 10, 20, 50, 100]
sum = reduce((lambda x, y: x + y), li)
print (sum)

output

193

Leave a Comment

Your email address will not be published.