>>> def f(x): return x % 2 != 0 and x % 3 != 0
>>> [i for i in range(2, 25) if f(i)]
[5, 7, 11, 13, 17, 19, 23]
>>> def cube(x): return x*x*x
>>> [cube(i) for i in range(1, 11)]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
@Breezer actually i**3 will call i.__pow__(3) and i*i*i i.__mul__(i).__mul__(i) (or something like that). With ints it doesn't matter but with numpy numbers/custom classes it might even produce different results.
Edit: The 99 percent figure is pulled directly from the Whats New In Python 3.0 page authored by Guido van Rossum.
So, for filter and map, you can wrap them with list() to see the results like you did before.
The functionality of map and filter was intentionally changed to return iterators, and reduce was removed from being a built-in and placed in functools.reduce.
The recommendation now is that you replace your usage of map and filter with generators expressions or list comprehensions. Example:
They say that for loops are 99 percent of the time easier to read than reduce, but I'd just stick with functools.reduce.
You are absolutely correct. I kept the function in the list comprehension examples to keep it looking similar to the filter/map examples.
You do not need to create extra functions in list comprehensions. Just use [i*i*i for i in range(1,11)]
i**3 is also equivalent of i*i*i