Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Difference between filter(function, sequence) and map(function, sequence)

I'm reading through the Python documentation to really get in depth with the Python language and came across the filter and map functions. I have used filter before, but never map, although I have seen both in various Python questions here on SO.

After reading about them in the Python tutorial, I'm confused on the difference between the two. For example, from 5.1.3. Functional Programming Tools:

>>> def f(x): return x % 2 != 0 and x % 3 != 0 ... >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17, 19, 23] 

and

>>> def cube(x): return x*x*x ... >>> map(cube, range(1, 11)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] 

These looked almost exactly the same in function to me, so I went into terminal to run Python interactively and tested out my own case. I used map for both the first and second instances above, and for the first one ( return x % 2 != 0 and x % 3 != 0 ) it returned a list of booleans rather than numbers.

Why does map sometimes return a boolean and other times the actual return value?

Can someone explain to me exactly the difference between map and filter?

like image 791
samrap Avatar asked Sep 22 '13 02:09

samrap


People also ask

What is difference between map function and filter function in Python?

The map function performs a transformation on each item in an iterable, returning a lazy iterable back. The filter function filters down items in an iterable, returning a lazy iterable back.

What is the difference between map and filter function?

map creates a new array by transforming every element in an array individually. filter creates a new array by removing elements that don't belong. reduce , on the other hand, takes all of the elements in an array and reduces them into a single value. Just like map and filter , reduce is defined on Array.

What's difference between map and filter in Python?

Map takes all objects in a list and allows you to apply a function to it whereas Filter takes all objects in a list and runs that through a function to create a new list with all objects that return True in that function.

What is the difference between MapReduce and filter functions in Python?

Python's reduce() function doesn't return a new sequence like map() and filter(). Instead, it returns a single value. The syntax is similar to the other two functions: reduce() applies the function to the elements of the sequence, from left to right, starting with the first two elements in the sequence.


2 Answers

list(map(cube, range(1, 11))) 

is equivalent to

[cube(1), cube(2), ..., cube(10)] 

While the list returned by

list(filter(f, range(2, 25))) 

is equivalent to result after running

result = [] for i in range(2, 25):     if f(i):         result.append(i) 

Notice that when using map, the items in the result are values returned by the function cube.

In contrast, the values returned by f in filter(f, ...) are not the items in result. f(i) is only used to determine if the value i should be kept in result.


In Python2, map and filter return lists. In Python3, map and filter return iterators. Above, list(map(...)) and list(filter(...)) is used to ensure the result is a list.

like image 158
unutbu Avatar answered Sep 28 '22 01:09

unutbu


filter(), as its name suggests, filters the original iterable and retents the items that returns True for the function provided to filter().

map() on the other hand, apply the supplied function to each element of the iterable and return a list of results for each element.

Follows the example that you gave, let's compare them:

>>> def f(x): return x % 2 != 0 and x % 3 != 0 >>> range(11) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> map(f, range(11))  # the ones that returns TRUE are 1, 5 and 7 [False, True, False, False, False, True, False, True, False, False, False] >>> filter(f, range(11))  # So, filter returns 1, 5 and 7 [1, 5, 7] 
like image 32
CT Zhu Avatar answered Sep 28 '22 01:09

CT Zhu