All Courses

Define three individual functions to implement the filter, map and reduce functions.

By Jennifer, 2 years ago
  • Bookmark
0

Implement the filter, map and reduce functions in python and give some examples

Filter
Map
Reduce
1 Answer
0

import functools

def f(x):  
  return x % 2 != 0 and x % 3 != 0        # number not divided by 2 and 3
print(list(filter(f, range(2,25))))       # filter function

def cube(x):
  return x*x*x                            # cube of number
print(list(map(cube, range(1,11))))       # map function

def add(x,y):             
  return x+y                              # addition of two number
print(functools.reduce(add, range(1,11))) # reduce fuction

Output

[5, 7, 11, 13, 17, 19, 23]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
55


Your Answer

Webinars

Why You Should Learn Data Science in 2023?

Jun 8th (7:00 PM) 289 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More