Sitemap

lambda Function…

1 min readJun 6, 2024

--

lambda function is also known as anonymous function in python , basically it is unnamed and one line function. This function can have any number of argument but only one expression.

lambda arguments : expression

Think about this kind of scenario , you need a small function for short time period like using map() , filter() , sorted() functions ,so lambda function comes in that time.

This procedure makes clean and efficient code.Now we look some use cases ,

Get kalindu ranathunga’s stories in your inbox

Join Medium for free to get updates from this writer.

01) Understanding how it works :

addition = lambda x,y : x+Y
result = addition(3,4)

02) lambda function with map() function

numbers = [ 1 , 2 , 3 , 4 ]
square_num = map(lambda x : x**2 , numbers)

03) lambda function with filter() function

numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
greater_than_five = filter(lambda x : x>5 , numbers)

--

--