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.
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 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] |
# 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] |




Iβm a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND