Python Lambda Functions


Why Use Lambda Functions?

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

Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number:

def myfunc(n):
      return lambda a : a * n

Use that function definition to make a function that always doubles the number you send in:

Example:1
Python program to double the value.
Sol:

#without user input
def myfunc(n):
  return lambda a : a * n

doub = myfunc(2)

print(doub(11))

Output:

22
>>>

Example:2
Python program to double the value (using user input).
Sol:

#without user input
def myfunc(n):
  return lambda a : a * n

doub = myfunc(2)
z=int(input("Enter the value to double "))
print(doub(z))

Output:

Enter the value to double 5
10
>>>

Example:3
Python program to print the square of value(without user input).
Sol:

#without user input
def myfunc(n):
  return lambda a : a ** n 

square = myfunc(2)
print(square(5))

Output:

25
>>>

Example:4
Python program to print square of a value(Using user input).
Sol:

#Using user input
def myfunc(n):
  return lambda a : a ** n 

square = myfunc(2)
z=int(input("Enter the value to square "))
print(square(z))

Output:

Enter the value to square 6
36
>>>

Example:5
Python program to print the cube of value(without user input).
Sol:

#without user input
def myfunc(n):
  return lambda a : a ** n 

cube = myfunc(3)
print(cube(5))

Output:

125
>>>

Example:6
Python program to print cube of a value(Using user input).
Sol:

#Using user input
def myfunc(n):
  return lambda a : a ** n 

cube = myfunc(3)
z=int(input("Enter the value to cube "))
print(cube(z))

Output:

Enter the value to cube 5
125
>>>


Important Use of Lambda Function in python

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:1
# Program to filter out only the even items from a list
Sol:

n = [1, 5, 4, 6, 8, 11, 3, 12]

n1 = list(filter(lambda x: (x%2 == 0) , n))

print(n1)

Output:

[4, 6, 8, 12]
>>>

Example:2
python to create a list of elements further display all even elements present in the list.
Sol:

n=[]
while True:
    a=int(input("Enter number to add in list "))
    n.append(a)
    ch=input("Like to add another element ")
    if(ch=='y' or ch=='Y'):
        continue
    else:
        break

print("Full list is ")
print(n)
print("list of even number in the list ")

n1 = list(filter(lambda x: (x%2 == 0) , n))

print(n1)

Output:

Enter number to add in list 25
Like to add another element y
Enter number to add in list 95
Like to add another element y
Enter number to add in list 36
Like to add another element y
Enter number to add in list 45
Like to add another element y
Enter number to add in list 22
Like to add another element y
Enter number to add in list 78
Like to add another element y
Enter number to add in list 81
Like to add another element y
Enter number to add in list 45
Like to add another element n
Full list is
[25, 95, 36, 45, 22, 78, 81, 45]
list of even number in the list
[36, 22, 78]
>>>

Example:3
# Program to filter out only the odd items from a list
Sol:

n = [11, 55, 24, 61, 8, 211, 3, 112,15]

n1 = list(filter(lambda x: (x%2 == 1) , n))

#Function call
print(n1)

Output:

[11, 55, 61, 211, 3, 15]
>>>

Example:4
python to create a list of elements further display all odd elements present in the list.
Sol:

n=[]
while True:
    a=int(input("Enter number to add in list "))
    n.append(a)
    ch=input("Like to add another element ")
    if(ch=='y' or ch=='Y'):
        continue
    else:
        break

print("Full list is ")
print(n)
print("list of odd number in the list ")

n1 = list(filter(lambda x: (x%2 == 1) , n))

print(n1)

Output:

Enter number to add in list 5
Like to add another element y
Enter number to add in list 25
Like to add another element y
Enter number to add in list 32
Like to add another element y
Enter number to add in list 4
Like to add another element y
Enter number to add in list 21
Like to add another element y
Enter number to add in list 95
Like to add another element n
Full list is
[5, 25, 32, 4, 21, 95]
list of odd number in the list
[5, 25, 21, 95]
>>>

Example:5
python to create a list of elements further display all multiples of 5 present in the list.
Sol:

n=[]
while True:
    a=int(input("Enter number to add in list "))
    n.append(a)
    ch=input("Like to add another element ")
    if(ch=='y' or ch=='Y'):
        continue
    else:
        break

print("Full list is ")
print(n)
print("list of multiple of 5 in the list ")

n1 = list(filter(lambda x: (x%5 == 0) , n))

print(n1)

Output:

Enter number to add in list 25
Like to add another element y
Enter number to add in list 36
Like to add another element y
Enter number to add in list 45
Like to add another element y
Enter number to add in list 44
Like to add another element y
Enter number to add in list 95
Like to add another element y
Enter number to add in list 78
Like to add another element y
Enter number to add in list 75
Like to add another element y
Enter number to add in list 41
Like to add another element y
Enter number to add in list 24
Like to add another element n
Full list is
[25, 36, 45, 44, 95, 78, 75, 41, 24]
list of multiple of 5 in the list
[25, 45, 95, 75]
>>>

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.

Example:1
# python Program to double each item in a list using map()
Sol:

n = [1,3,2,6,5,8,7,3,10]

n1 = list(map(lambda x: x * 2 , n))

print("List with elements")
print(n)
print("List with double of elements ")
print(n1)

Output:

List with elements
[1, 3, 2, 6, 5, 8, 7, 3, 10]
List with double of elements
[2, 6, 4, 12, 10, 16, 14, 6, 20]
>>>

Example:2
# Python Program to square each item in a list using map()
Sol:

n = [1,3,2,6,5,8,7,3,10]

n1 = list(map(lambda x: x ** 2 , n))

print("List with elements")
print(n)
print("List with square of elements ")
print(n1)

Output:

List with elements
[1, 3, 2, 6, 5, 8, 7, 3, 10]
List with square of elements
[1, 9, 4, 36, 25, 64, 49, 9, 100]
>>>

Example:3
# Python Program to calculate cube each item in a list using map()
Sol:

n = [1,3,2,6,5,8,7,3,10]

n1 = list(map(lambda x: x ** 3 , n))

print("List with elements")
print(n)
print("List with cube of elements ")
print(n1)

Output:

List with elements
[1, 3, 2, 6, 5, 8, 7, 3, 10]
List with cube of elements
[1, 27, 8, 216, 125, 512, 343, 27, 1000]
>>>

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Tutorials

Technical Questions and Important programs

Interview Questions

C Programming
C++ Programming
Basic Python Tutorial
Advanced Python Tutorial
C Language
C++ Programming
Python programming
C Important Programs
C Interview Questions
C++ Interview Questions
Python Interview Questions
HTML Interview Questions