0% found this document useful (0 votes)
14 views

PYTHON Map, Filter, Ruduce

Uploaded by

ritesh kushwah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

PYTHON Map, Filter, Ruduce

Uploaded by

ritesh kushwah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PYTHON

Python map() function


map() function returns a map object(which is an iterator)
of the results after applying the given function to each
item of a given iterable (list, tuple etc.)
Python map() Function Syntax
Syntax: map(fun, iter)
Parameters:
• fun: It is a function to which map passes each element
of given iterable.
• iter: It is iterable which is to be mapped.
NOTE: You can pass one or more iterable to the map()
function.
Returns: Returns a list of the results after applying the
given function to each item of a given iterable (list,
tuple etc.)
map() Arguments
The map() function takes two arguments:
function - a function
iterable - an iterable like sets, lists, tuples, etc
You can pass more than one iterable to the
map() function.
Demonstration of map() in Python
In this example, we are demonstrating the map() function in Python.

# Python program to demonstrate working


# of map.

# Return double of n
def addition(n):
return n + n

# We double all numbers using map()


numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))

Output [2, 4, 6, 8]
Demonstration of map() in Python
map() with Lambda Expressions
We can also use lambda expressions with map to
achieve above result. In this example, we are using
map() with lambda expression.

# Double all numbers using map and lambda

numbers = (1, 2, 3, 4)
result = map(lambda x: x + x, numbers)
print(list(result))

Output [2, 4, 6, 8]
Demonstration of map() in Python
Add Two Lists Using map and lambda
In this example, we are using map and lambda to add
two lists.
# Add two lists using map and lambda

numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]

result = map(lambda x, y: x + y, numbers1, numbers2)


print(list(result))

Output [5, 7, 9]
Demonstration of map() in Python
Add Two Lists Using map and lambda
In this example, we are using map and lambda to add
two lists.
# Add two lists using map and lambda

numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]

result = map(lambda x, y: x + y, numbers1, numbers2)


print(list(result))

Output [5, 7, 9]
Demonstration of map() in Python
Example : Working of map()
def calculateSquare(n):
return n*n

numbers = (1, 2, 3, 4)
result = map(calculateSquare, numbers)
print(result)

# converting map object to set


numbersSquare = set(result)
print(numbersSquare)

Output
<map object at 0x7f722da129e8>
{16, 1, 4, 9}
In the above example, each item of the tuple is squared.
filter() in python
The filter() method filters the given sequence
with the help of a function that tests each
element in the sequence to be true or not.
Python filter() Syntax
Syntax: filter(function, sequence)
• function: function that tests if each element
of a sequence is true or not.
• sequence: sequence which needs to be
filtered, it can be sets, lists, tuples, or
containers of any iterators.
filter() in python
# function that filters vowels
def fun(variable):
letters = ['a', 'e', 'i', 'o', 'u']
if (variable in letters):
return True
else:
return False

# sequence
sequence = ['g', 'e', 'e', 'j', 'k', 's', 'p', 'r']

# using filter function


filtered = filter(fun, sequence)

print('The filtered letters are:')


for s in filtered:
print(s)

output:
The filtered letters are: e e
Filter Function in Python with Lambda
# a list contains both even and odd numbers.
seq = [0, 1, 2, 3, 5, 8, 13]

# result contains odd numbers of the list


result = filter(lambda x: x % 2 != 0, seq)
print(list(result))

# result contains even numbers of the list


result = filter(lambda x: x % 2 == 0, seq)
print(list(result))

Output:
[1, 3, 5, 13]
[0, 2, 8]
reduce() in Python

The reduce(fun,seq) function is used to apply a particular


function passed in its argument to all of the list elements
mentioned in the sequence passed along.This function is
defined in “functools” module.
Working :
• At first step, first two elements of sequence are picked and
the result is obtained.
• Next step is to apply the same function to the previously
attained result and the number just succeeding the second
element and the result is again stored.
• This process continues till no more elements are left in the
container.
• The final returned result is returned and printed on
console.
reduce() in Python
# python code to demonstrate working of reduce()

# importing functools for reduce()


import functools

# initializing list
lis = [1, 3, 5, 6, 2]

# using reduce to compute sum of list


print("The sum of the list elements is : ", end="")
print(functools.reduce(lambda a, b: a+b, lis))

# 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))

Output
The sum of the list elements is : 17
The maximum element of the list is : 6
reduce() in Python
Using Operator Functions
reduce() can also be combined with operator functions to achieve the similar functionality as with lambda functions and makes
the code more readable.

# python code to demonstrate working of reduce()


# using operator functions

# importing functools for reduce()


import functools

# importing operator for operator functions


Output
import operator The sum of the list elements is : 17
The product of list elements is : 180
# initializing list The concatenated product is : geeksforgeeks
lis = [1, 3, 5, 6, 2]

# using reduce to compute sum of list


# using operator functions
print("The sum of the list elements is : ", end="")
print(functools.reduce(operator.add, lis))

# using reduce to compute product


# using operator functions
print("The product of list elements is : ", end="")
print(functools.reduce(operator.mul, lis))

# using reduce to concatenate string


print("The concatenated product is : ", end="")
print(functools.reduce(operator.add, ["geeks", "for", "geeks"]))

You might also like