>>> n = [1,2,3,4]
>>> filter(lambda x:x>3,n)
<filter object at 0x0000000002FDBBA8>
>>> len(filter(lambda x:x>3,n))
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
len(filter(lambda x:x>3,n))
TypeError: object of type 'filter' has no len()
I could not get the length of the list I got. So I tried saving it to a variable, like this...
>>> l = filter(lambda x:x>3,n)
>>> len(l)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
len(l)
TypeError: object of type 'filter' has no len()
Instead of using a loop, is there any way to get the length of this?
Converting a filter to a list will take extra memory, which may not be acceptable for large amounts of data. You can find length of the filter object without converting it to a list:
sum(1 for _ in filter(lambda x: x > 3, n))
The docs for python 3 say it returns an iterator
In python 2 it returned a list: see here. You will need to iterate the filter object to find its length.
Generally,
filter
andreduce
are not pythonic.@arshajii metioned this solution:
This is quite simple, but is not describing what you exactly want to do, and it makes a list that may use some additional memory. A better solution is using
sum
with generator:(See more about generator here: https://www.python.org/dev/peps/pep-0289/#rationale)
However,
sum
with generator is actually slower in most cases because of the implementation (tested in CPython 3.6.4):This is an old question, but I think this question needs an answer using the map-reduce ideology. So here:
This is especially good if
n
doesn't fit in the computer memory.You have to iterate through the filter object somehow. One way is to convert it to a list:
But that might defeat the point of using
filter()
in the first place, since you could do this more easily with a list comprehension:Again,
len(l)
will return the length.