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
?
map
andfilter
function in python is pretty different because they perform very differently. Let's have a quick example to differentiate them.map function
Let's define a function which will take a string argument and check whether it presents in vowel letter sequences.
Now let's create a map function for this and pass some random string.
And yes it's equivalent to following
simply it will print
filter function
Now let's create a
filter
function for this and pass some random string.filter
as the name implies, filters the original iterable and retents the items that return True for the function provided to the filter function.Simply it will print
Fork it here for future reference, if you find this useful.
filter(function, iterable) function (pointer, like in C) return boolean type
map(function, iterable) function (pointer, like in C) return e.g. int
Filter--Returns the true value's position
Output
Values are [10, 20, 1]
Map--Returns the actual result
Output
Values are [20, 40, 0, 2]
Reduce--Take the first 2 items in the list,then calls function, In next function call,the result of previous call will be 1st argument and 3rd item in list will be 2nd argument
Output
Values of var_b [20, 40, 0, 2]
Values of var_c 160
filter()
, as its name suggests, filters the original iterable and retents the items that returnsTrue
for the function provided tofilter()
.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:
is equivalent to
While the list returned by
is equivalent to
result
after runningNotice that when using
map
, the items in the result are values returned by the functioncube
.In contrast, the values returned by
f
infilter(f, ...)
are not the items inresult
.f(i)
is only used to determine if the valuei
should be kept inresult
.In Python2,
map
andfilter
return lists. In Python3,map
andfilter
return iterators. Above,list(map(...))
andlist(filter(...))
is used to ensure the result is a list.