16_Lambda Functions
16_Lambda Functions
Learning objective
• What is Lambda Function
• Map function
• Filter function
• Reduce function
What is Lambda Function
• Small, anonymous function defined using the lambda keyword.
• They are often used for short operations that don't require a full
function definition.
• Lambda forms can take any number of arguments but return just
one value in the form of an expression.
Simplest form of Lambda Functions
Syntax:
lambda arguments: expression
---------------------------------------
addition = lambda a, b: a + b
print(addition(40,50))
cube = lambda n1 : n1 * n1 * n1
print(cube(10))
Example of Regular and Lambda Function
Program to add 2 numbers using Program to add 2 numbers using
Function lambda Function
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
print(list(squared_numbers))
Squaring each element in a list using lambda
function
numbers = [1, 2, 3, 4, 5]
print(list(squared_numbers_lambda))
Example: Converting strings to uppercase
words = ["apple", "banana", "cherry"]
print(list(uppercase_words))
Example: Adding corresponding elements of 2
lists
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print(list(sum_of_lists))
filter() Function
• Built-in function that allows you to construct an iterator from
elements of an iterable for which a function returns True.
• Used to apply a function to each element of an iterable (like a list
or tuple) and return another iterable containing just the elements
for which the function brings True back.
filter(function, iterable)
• A function that tests whether each element of an iterable returns
True or False.
• iterable: An iterable (e.g., a list, tuple, etc.).
Filtering even numbers from a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def is_even(x):
return x % 2 == 0
print(list(even_numbers))
Using lambda with filter
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(even_numbers)
Filtering words with length greater than 4
words = ["apple", "banana", "kiwi", "pear", "orange"]
def is_long(word):
return len(word) > 4
print(list(long_words))
Filter out negative numbers from a list
numbers = [ 1, -2, 3, -4, 5, -6 ]
print(result)
reduce() function
• Built-in function that is part of the functools module.
• It is used to apply a binary function (a function taking two
arguments) cumulatively to the items of an iterable, from left to
right, so as to reduce the iterable to a single cumulative result.
• The basic syntax of the reduce() function is as follows:
functools.reduce(function, iterable)
function: A binary function that takes two arguments.
iterable: An iterable (e.g., a list, tuple, or other iterable).
Summing up elements in a list
from functools import reduce
numbers = [1, 2, 3, 4, 5]
print(sum_result)
Calculating the product elements in a list
from functools import reduce
numbers = [2, 3, 4, 5]
print(product_result)
Concatenating strings in a list
from functools import reduce
print(concatenated_result)
Finding the maximum element in a list
from functools import reduce
numbers = [4, 2, 8, 6, 7]
print(max_result)
reduce() function
• In each example, the reduce() function applies the specified
binary function (lambda x, y: ...) to the elements of the iterable,
accumulating a single result.
[20, 60, 65, 50, 19, 17, 65, 20, 50, 54]