Python Functions ( lambda function, map function, filter function)

Function is a block of code which performs specific task.

Syntax of function

def function_name(parameters):

  //body

How to call a function

#syntax for call functions

function_name(parameters)

What is lambda function?

Lambda function is anonymous function. No function name when we use lambda function. Lambda function has many argument but we can define only single expressions. To define a lambda function we will use lambda keyword.

#Syntax of lambda function


g = lambda x:x*x*x
In: g(10)
Output: 1000

Lambda function with filters

If we need to filter some values from list then we can use filter function with lambda function

#Example of filter function with lambda

li = [1,2,3,4,5,6,7]

#Suppose we need to find out all odd values from that list

odd_list = list(filter( lambda x:(x%2!=0), li))
Output: [1,3,5,7]

Lambda function with map

If we need to mapping some values with function then we will use map function

#Example of map function

li = [1,2,3,4,5,6,7]
Result_list = list(map(lambda x: x*2, li))
Output: [2,4,6,8,10,12,14]

Leave a Comment

Your email address will not be published. Required fields are marked *