Lambda Function, also referred to as ‘Anonymous function’ is same as a regular python function but can be defined without a name. While normal functions are defined using the def keyword, anonymous functions are defined using the lambda keyword. However, they are restricted to single line of expression. They can take in multiple parameters as in regular functions.
Python Lambda Function Syntax:
lambda arguments: expression
- This function can have any number of arguments but only one expression, which is evaluated and returned.
- One is free to use lambda functions wherever function objects are required.
- You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.
- It has various uses in particular fields of programming besides other types of expressions in functions.
Example of Lambda Function in python
Here is an example of lambda function that doubles the input value.
# Program to show the use of lambda functions
double = lambda x: x * 2
Need for Lambda Functions
There are at least 3 reasons:
- Lambda functions reduce the number of lines of code when compared to normal python function defined using def But this is not exactly true because, even functions defined with def can be defined in one single line. But generally, def functions are written in more than 1 line.
- They are generally used when a function is needed temporarily for a short period of time, often to be used inside another function such as filter, map and reduce.
- Using lambda function, you can define a function and call it immediately at the end of definition. This can’t be done with def
Parameterless Lambda Function
The following is an example of the parameterless lambda function.
Example: Parameterless Lambda Function
greet = lambda : print(‘Hello World!’)
We can declare a lambda function and call it as an anonymous function, without assigning it to a variable.
(lambda x: x*x)(5)
Above, lambda x: x*x defines an anonymous function and call it once by passing arguments in the parenthesis (lambda x: x*x)(5).
Use of Lambda Function in python
We use lambda functions when we require a nameless function for a short period of time.
In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments). Lambda functions are used along with built-in functions like filter(), map() etc.
Example use with filter()
The filter() function in Python takes in a function and a list as arguments.
The function is called with all the items in the list and a new list is returned which contains items for which the function evaluates to True.
Here is an example use of filter() function to filter out 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))
[4, 6, 8, 12]
Example use with map()
The map() function in Python takes in a function and a list.
The function is called with all the items in the list and a new list is returned which contains items 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))
[2, 10, 8, 12, 16, 22, 6, 24]
Example use with reduce()
reduce() function performs a repetitive operation over the pairs of the elements in the list. Pass the lambda function and the list as arguments to the reduce() function. For using the reduce() function, you need to import reduce from functools librray.
|# Using lambda inside reduce
from functools import reduce
list1 = [1,2,3,4,5,6,7,8,9]
sum = reduce((lambda x,y: x+y), list1)
Python Tutorial – Learn Python
What is Python? What makes Python so Powerful?
Variables in Python – Constant, Global & Static Variables
Namespacing and Scopes in Python
TUPLE Data Structure in PYTHON
Differences between List and Tuple
DICTIONARY Data Structure in PYTHON
Database Programming in Python
What is Multithreading in Python?
Python Exception Handling Using try, except and finally statement