Lambda Function Overview

Python me Lambda Function ka use short lines of code ke liye hota hai. Lambda function me kai sare arguments diye ja sakte hain lekin expression ek hi use kar sakte hain aur yah single single of code se result bhi return kar deta hai.

Lambda Function ka Syntax:
lambda arguments : expression

Lambda Function Examples

#single argument
a= lambda x : x*3
print(a(4))

#two arguments
a = lambda x,y : x*y
print(a(2,10))

#more than two arguments
a = lambda x,y,z : x*y+z
print(a(2,10,20))

Lambda Function ka filter() ke sath use

filter() function kisi bhi input ko condition ke according perform karke output provide karta hai. Jaise agar hum number of sequence se odd numbers ko filter out kara chahte hain. Example me filter() lambda ko ek function aur “num” list ko ek argument ki tarah treat kar raha hai aur sath hi output bhi list ke format me de raha hai.

num = [34,54,63,32,21,45,67,89,88,90]
odd_num = list(filter(lambda x: (x%2 != 0) , num))
print(odd_num)

Lambda Function ka map() ke sath use

map() function list ke har ek element ko lambda function ke according modify karega aur apna output bhi list ke format me return karega. 

num = [34,54,63,32,21,45,67,89,88,90]
odd_num = list(map(lambda x: (x*2) , num))
print(odd_num)