Lambda Expression

Lambda Expression/function In Python

In this artice we will learn about lambda expression in python and also we will learn how and where we can use it.

Lambda Expression

What is Lambda Expression:-

Lambda expression/function is an anonymous function.
What is anonymous Function:-
A Anonymous function is a function that is defined with-out name.

Every anonymous function you define in Python will have 3 essential parts:

Python Lambda expression of consisted of three component.
The lambda keyword.
Argument-list: it can be empty or non-empty
The function body:it contians expression and statment for lambda expression.

Syntax of Lambda Function in python
lambda arguments: expression

Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.

Write Lambda Expression for following given function

def sayMessage(message):
print(“Hello”+message)

Now let’s call the function

sayMessage()

Lambda Expression for following given function
sayMessage=lambda message:print(“hello”+message)

Now let’s call the function

sayMessage()

Define Lambda Expression which takes two arguments and sum of that arguments.

Defing a function with-out lambda expression

def sum(a,b):
return a+b

Now let’s call the function

print(sum(10,20))

Defing a function with lambda expression

sum=lambda x,y:x+y

Now let’s call the lambda function

print(sum(10,20))

Define Lambda Expression which takes one arguments and return the square of that arguments.

Defing a function with-out lambda expression

def square(x):
return x*x

Now let’s call the function

print(square(2))

Defing a function with lambda expression

square=lambda x:x*x

Now let’s call the lambda function

print(square(2))

Define Lambda Expression which takes one arguments as a string and return the length of that arguments.

Defing a function with-out lambda expression

def getLength(s):
return len(s)

Now let’s call the function

print(getLength(“python”))

Defing a function with lambda expression

getLength=lambda s:len(s)

Now let’s call the lambda function

print(getLength(“python”))#output 6

Use of Lambda Function in python
We use lambda functions when we require a nameless function for a short period of time.

The power of lambda is better shown when you them as an anonymous function inside another function.

The filter() function in Python takes two parameter first parameter it will take as the name of a function and second parameter it will take iterable object.

Syntax:
filter(func-name,iterable-object)

filter function offers an elegant way to filter out all the elements of a sequence(iterable object) based on provied function. It will collect only those element from the given sequence for which the function returns True.

Program to filter out only the even items from a list with-out filter function

my_list=[1,2,3,4,5,6,7]
even_list=[]
for i in my_list:
if i%2==0:
even_list.append(i)
print(even_list)#output [2, 4, 6]

Program to filter out only the even items from a list with filter function

my_list=[1,2,3,4,5,6,7]
def even(n):
if n%2==0:
return True
else:
return False

Now let’s Use the filter function

even_list=list(filter(even,my_list))

print(even_list)#output [2, 4, 6]

Program to filter out only the even items from a list with filter function by passing lambda expression to the filter function

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

Now let’s Use the filter function

even_list=list(filter(lambda n:n%2==0,my_list))

print(even_list)#output [2, 4, 6]

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))

print(new_list)

Use of lambda() with reduce()

The reduce() function in Python takes in a function and a list as argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list. This is a part of functools module. Example:

Python code to illustrate

reduce() with lambda()

to get sum of a list

from functools import reduce
li = [5, 8, 10, 20, 50, 100]
sum = reduce((lambda x, y: x + y), li)
print (sum)

ex-1
inp_list = [‘t’,’u’,’t’,’o’,’r’,’i’,’a’,’l’]
result = list(filter(lambda x: x!=’t’ , inp_list))

print(result)

Leave a Comment

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