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
?