You are on page 1of 8

Programming of scientific

computing(PYTHON)
IU214122OO27

PYTHON LABORATORY-5

Introduction about lambda Function, map, filter and reduce function

Lambda function:

Lambda is a keyword in Python used to define functions, more specifically


Anonymous Functions, and such functions are known as Lambda Functions or
Lambda Expressions

Map function:

What If I ask you to find a square of the first five odd numbers. You would probably
write something like the following:

squares = []
for n in rangefl, 10, 2):
squares.append(n ** 2)
The loop iterates through 1 to 10 with a step size of 2 and appends the square of
each number to squares. The approach shown above can be improved using list
comprehension but that too will use for loop. So, there's a faster way to do the
same without using explicit for loop. Python's map function does that. It transforms
the given iterable based on the given condition.

Filter function:

Let's say you want to find the odd numbers from a given list. A quick way to
approach this problem is to use a for loop inside a list comprehension.

nums = [1, 34, 23, 56, 89, 44, 92]


odd_nums = [num for num in nums if num % 2 != 0]
The loop iterates through nums and stores every odd number. The conditional
statement here filters out the even numbers and returns only the odd numbers.
This kind of functionality is known as filtering.

Python's filter does the same in a faster way. It has the same syntax as the map.

IU2141220027
Programming of scientific
computing(PYTHON)
filter(function, iterable)
It works similarly to the map. It returns a filter object, unlike a map object.

Let us re-implement the above example using the filter function.

def find_odd(x):
if x % 2 1=0:
return x
nums = [1, 34, 23, 56, 89, 44, 92]
odds = Iist(filter(find_odd, nums))
print(odds)
# Output: [1, 23, 89]
Let us convert this find_odd function to a lambda function and rewrite the code to
make it shorter.

nums = [1, 34, 23, 56, 89, 44, 92]


odds = Iist(filter(lambda x: x % 2 != 0, nums))
print(odds)
# Output: [1, 23, 89]
All the items of an iterable for which the lambda function evaluates to true will get
added to the odd.

Reduce function:

Lets say you want to compute a sum of the first five integers. You can write
something like the following:

nums = [1, 2, 3, 4, 5]
summ = 0
for num in nums:
summ += num
summ
# Output: 15
The loop iterates through nums and keeps adding all the numbers to summ. Again
to make it pythonic, we have a function, i.e. Reduce.

The syntax for the reduce is as follows: reduce(function, iterable, [, initializer])


The reduce applies a function cumulatively on all the items of an iterable and

IU2141220027
Programming of scientific
computing(PYTHON)
returns a single value. Don't worry if you haven't get it yet.

Let us re-implement the above example with the reduce function.

from functools import reduce


nums = [lz 2Z 3Z 4Z 5]
summ = reduceflambda xz y: x + yz nums)
summ
# Output: 15
PRACTICAL

1. Write a Python program to find the triple of all numbers in a given list of
integers.

a = [1Z2Z3Z4Z5]

filt = map(lambda x:x**3za)

print("\n Triple Of The Elements is: ") print(list(filt))

Output:

Triple Of The Elements is:

[1, 8, 27, 64, 125]

IU2141220027
Programming of scientific
computing(PYTHON)

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

a = [1,2,3]

b = [2,3,4]

c = [6,7,8]

ans = map(lambda x,y,z: x+y+z,a,b,c)

print)" \n Sum of three given list is:")

print(list)ans))

Output:
Sum of three given list is:

ft 12,15]

IU2141220027
Programming of scientific
computing(PYTHON)

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


using python map.

names = ["Aarya","Nainir,"Kirtan","Dev"]

listify = list(map(list, names)) print("\n After listify the list of strings are:")
print(listify)

Output:

After listify, the list of strings are:


Programming of scientific
computing(PYTHON)

4. 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.

base = [1,2,3,4,5,6]
index = [2,4,1,2,3,3]

result = list(map(pow, base, index))

print("\n Power of said number in bases raised to the corresponding number


in the index:")

print(result)

Output:

Power of said number in bases raised to the corresponding number in the


index:

IU2141220027
Programming of scientific
computing(PYTHON)

5. Write a python program using a filter function to filter out positive


numbers from the given list.

a = [1Z-2Z3Z-4Z-5Z6Z7Z-8Z9]

pos = listffilter(lambda x:x>Oza))

print("\n Positive Numbers from the List are:")

print(pos)

Output:

Positive Numbers from the List are:

[1, 3Z 6Z1, 9]

6. Write a python program using a filter function to filter out even


numbers
from the given list.

a = [1Z-2Z3Z-4Z-5Z6Z7Z-8Z9]

pos = list(filter(lambda x:x%2==0za))


print("\n Even Numbers from the List are:") print(pos)

Output:

Even Numbers from the List are:[-2, -4, 6, -8]

IU2141220027
Programming of scientific
computing(PYTHON)

7. Write a Python program to filter all the vowels from a given string.

str = "This is Indus University!"

printf'The String isstr)

print("\n The Vowels from the List are:")

for i in str:
•ri • Il • ii • p| • ii •ll\
if (i== a or i== e or i== i or i== o or i== u ):

print(i)

Output:

The String is : This is Indus University!

The Vowels from the List are:

IU2141220027

You might also like