This question already has an answer here:
I am learning the concept of filters in Python. I am running a simple code like this.
>>> def f(x): return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
But instead of getting a list, I am getting some message like this.
<filter object at 0x00FDC550>
What does this mean? Does it means that my filtered object i.e list to come out is stored at that memory location? How do I get the list which I need?
It's an iterator returned by the filter function.
If you want a list, just do
Nonetheless, you can just iterate over this object with a
for
loop.It looks like you're using python 3.x. In python3,
filter
,map
,zip
, etc return an object which is iterable, but not a list. In other words,is equivalent to:
I think it was changed because you (often) want to do the filtering in a lazy sense -- You don't need to consume all of the memory to create a list up front, as long as the iterator returns the same thing a list would during iteration.
If you're familiar with list comprehensions and generator expressions, the above filter is now (almost) equivalent to the following in python3.x:
As opposed to:
in python 2.x