0% found this document useful (0 votes)
14 views5 pages

Unit 1 CHP 3

Python lambda functions are anonymous functions that can be used for filtering, mapping, and reducing iterables. Lambda functions are defined using the lambda keyword followed by arguments and an expression. They are useful for quick, one-time uses instead of defining a separate function. Lambda functions can be passed to functions like filter() to filter lists based on criteria, map() to transform lists, and reduce() to iteratively reduce a list to a single value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Unit 1 CHP 3

Python lambda functions are anonymous functions that can be used for filtering, mapping, and reducing iterables. Lambda functions are defined using the lambda keyword followed by arguments and an expression. They are useful for quick, one-time uses instead of defining a separate function. Lambda functions can be passed to functions like filter() to filter lists based on criteria, map() to transform lists, and reduce() to iteratively reduce a list to a single value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lambda Function

1. Python Lambda Functions are anonymous function means that the function is without
a name.
2. def keyword is used to define a normal function in Python. Similarly,
the lambda keyword is used to define an anonymous function in python.
3. Python Lambda Function Syntax
Syntax: lambda arguments: expression
This function can have any number of arguments but only one expression,
which is evaluated and returned.
4. Example:
a. Lambda function to print hello world
>>>a = lambda : print('Hello World')
we have defined a lambda function and assigned it to the variable named a
To execute this lambda function, we need to call it. Here's how we can call the
lambda function
# call the lambda
>>>a()
The lambda function above simply prints the text 'Hello World'.
Note: This lambda function doesn't have any arguments
b. lambda Function with an Argument
lambda function that accepts name of the user as an argument and print it
# lambda that accepts one argument
a = lambda name : print('Hey there,', name)

# lambda call
a('John')
c. Lambda functions with multiple arguments
x = lambda a, b : a * b
print(x(5, 6))
d. Area of circle using lambda function
import math
area = lambda r : math.pi*r*r
print(area(5.1))

Difference between Lambda function and normal function

Using lambda() Function with filter()


1. The filter() function returns an iterator where the items are filtered through a function
to test if the item is accepted or not.
2. Syntax
filter(function, iterable)
Parameter Description
function A Function to be run for each item in the iterable
iterable The iterable to be filtered
3. Example:
a. Filter the array, and return a new array with only the values equal to or above 18:
ages = [5, 12, 17, 18, 24, 32]
def myFunc(x):
if x < 18:
return False
else:
return True
adults = filter(myFunc, ages)
for x in adults:
print(x)
#using lambda
ages = [5, 12, 17, 18, 24, 32]
adults = filter(lambda x:x>=18, ages)
for x in adults:
print(x)

Using lambda() Function with map()


1. The map() function in Python takes in a function and a list as an argument.
2. The function is called with a lambda function and a list and a new list is returned
which contains all the lambda-modified items returned by that function for each
item.
3. Syntax of map() function
map(function, iterable, [iterable1, iterable2, ...])
4. Example:
a. Multiply all elements of a list by 2 using lambda and map() function
# Python code to illustrate
# map() with lambda()
# to get double of a list.
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(map(lambda x: x*2, li))
print(final_list)
b. Square
# Create numbers list
numbers = [2, 4, 5, 6, 3]
# Use map() with lambda that takes single argument
squared_result = list(map(lambda x: x**2, numbers))
print(squared_result)

c. Transform all elements of a list to upper case using lambda and map()
function
animals = ['dog', 'cat', 'parrot', 'rabbit']
uppered_animals = list(map(lambda animal: animal.upper(), animals))
print(uppered_animals)

d. Multiple arguments
# Create two lists with numbers
numbers1 = [2, 4, 5, 6, 3]
numbers2 = [1, 3, 2, 2, 4]
# Create lambda function that takes two arguments
add_fun = lambda x, y: x+y
add_result = list(map(add_fun, numbers1, numbers2))
print(add_result)
# Output:
# [3, 7, 7, 8, 7]
Here, the lambda function takes two arguments x, y and adds their
values. The map() function applies this lambda function to each
item of the numbers1 & numbers2 lists, and the result is a new
iterator containing the sum of both numbers element -wise.

Using lambda() Function with reduce()


1. The reduce() function in Python takes in a function and a list as an argument.
2. The function is called with a lambda function and an iterable and a new reduced
result is returned. This performs a repetitive operation over the pairs of the iterable.
3. The reduce() function belongs to the functools module.
4. Syntax:
reduce(function, iterable )
5. Example:
a. A sum of all elements in a list using lambda and reduce() function
from functools import reduce
li = [5, 8, 10, 20, 50, 100]
sum = reduce((lambda x, y: x + y), li)
print(sum)
Here the results of the previous two elements are added to the next element and
this goes on till the end of the list like (((((5+8)+10)+20)+50)+100).

b. Find the maximum element in a list using lambda and reduce() function
import functools
# initializing list
lis = [1, 3, 5, 6, 2, ]
# using reduce to compute maximum element from list
print("The maximum element of the list is : ", end="")
print(functools.reduce(lambda a, b: a if a > b else b, lis))

You might also like