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

Lab - 5

This document discusses Python lambda, map, filter and reduce functions. It provides examples of using lambda functions, the map function to apply a function to iterable elements, the filter function to filter elements of an iterable based on a condition, and the reduce function to reduce an iterable to a single value. It then gives examples of using map, filter and lambda functions to triple numbers in a list, add three lists, listify a list of strings, filter positive/even numbers from a list, and filter vowels from a string.

Uploaded by

anany ukani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Lab - 5

This document discusses Python lambda, map, filter and reduce functions. It provides examples of using lambda functions, the map function to apply a function to iterable elements, the filter function to filter elements of an iterable based on a condition, and the reduce function to reduce an iterable to a single value. It then gives examples of using map, filter and lambda functions to triple numbers in a list, add three lists, listify a list of strings, filter positive/even numbers from a list, and filter vowels from a string.

Uploaded by

anany ukani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Aayush Desai IU21412200

Lab - 5

Python Lambda, Map , Filter and Reduce functions

• Introduction about lambda Function:



A lambda function is a small anonymous function.

A lambda function can take any number of arguments, but can only have
one expression.
• Syntax
lambda arguments : expression
• Example:
Add 10 to argument a, and return the result:

x = lambda a : a + 10
print(x(5))

• Introduction about map function:


• Map in Python is a function that works as an iterator to return a result after applying
a function to every item of aniterable (tuple, lists, etc.).
• It is used when you want to apply a single transformation function to all the
iterable elements.
• The iterable and function are passed as arguments to the map in Python.

# Defining a function
def mul(i):
return i * i

# Using the map function


x = map(mul, (3, 5, 7, 11, 13))
print (x)
print(list(x))
Output:

5IT Python
Aayush Desai IU21412200

• Introduction about filter function:


• Filter() is a built-in function in Python.
• The filter function can be applied to aniterable such as a list or a dictionary and create
a new iterator.
• This new iterator can filter out certain specific elements based on the condition that
you provide very efficiently.

• Note: Aniterable in Python is an object that you can iterate over. It is possible to
loop over aniterable and return items that are in it.

• The syntax for the filter() function in Python is -

filter(function, iterable)

• Introduction about reduce function:


• Python’s reduce() is a function that implements a mathematical technique called
folding or reduction.
• reduce() is useful when you need to apply a function to aniterable and reduce it to
a single cumulative value.
• Python’s reduce() is popular among developers with a functional
programming background, but Python has more to offer.

• Write a Python program to triple all numbers in a given list of integers.

nums = {1,2,3,4,5}
m = map(lambda x : x*3 , nums)
print(f"The triple of all numbers in a given list of integers are: {list(m)}")

O/P:
The triple of all numbers in a given list of integers are: [3, 6, 9, 12, 15]

5IT Python
Aayush Desai IU21412200

• Write a Python program to add three given lists using Python map
and lambda.

num1 = [1,2,3]
num2 = [4,5,6]
num3 = [7,8,9]
m = map(lambda x,y,z : x+y+z , num1,num2,num3)
print(f"The addition of all given lists are: {list(m)}")

O/P:
The addition of all given lists are: [12, 15, 18]

• Write a Python program to listify the list of given strings individually


using Python map.

Animal = ["Dog","Cat","Lion"]
m = list(map(list,Animal))
print(f"listify the list of given strings are: {m}")

O/P:
listify the list of given strings are: [['D','o','g'], ['C','a','t'], ['L','i','o', 'n']]

• Write a Python program to create a list containing the power of said


number in bases raised to the corresponding number in the index
using Python map.

num=[10,20,30]
index=[1,2,3]
m=list(map(pow,num,index))
print(f"Power of the numbers are: {m}")

O/P:
Power of the numbers are: [10, 400, 27000]

5IT Python
Aayush Desai IU21412200

• Write a python program using a filter function to filter out


positive numbers from the given list.

num=[34,0,-1,53]
m=list(filter(lambda x: x>0 ,num))
print(f"Positive numbers in the list: {m}")

O/P:
Positive numbers in the list: [34, 53]

• Write a python program using a filter function to filter out even


numbers from a given list.

num=[1,2,3,4,5,6,7,8,9,10]
m=list(filter(lambda x: x%2==0 ,num))
print(f"Even numbers in the list: {m}")

O/P:
Even numbers in the list: [2, 4, 6, 8, 10]

• Write a python program to filter all the vowels from a given


string. alpha=['a','b','e','f','i','j','o']
defm(alpha):
vowels=['a','e','i','o','u']
if (alpha in vowels):
return True
else:
return False
m = filter(m,alpha)
for vowels in m:
print(f"Vowel in the list: {vowels}")

O/P:
Vowel in the list: a
Vowel in the list: e
Vowel in the list: i
Vowel in the list: o

5IT Python
Aayush Desai IU21412200

5IT Python

You might also like