Is the function filter deprecated?
steve+comp.lang.python at pearwood.info
Wed Apr 6 21:06:06 EDT 2011
On Wed, 06 Apr 2011 19:20:31 -0400, Jabba Laci wrote:
> I tried Pylint today and it gave me a warning for the function "filter".
> Is it deprecated?
> Is the usage of list comprehensions encouraged?
Certainly, but list comprehensions are not necessarily equivalent to
filter. In Python 3, filter is lazy and is closer to generator
expressions than list comprehensions. In Python 2, filter is equivalent
to a list comp.
> transformation is not complicated, by the way: replace "filter( func,
> seq )" with "[ x for x in seq if func(x) ]" .
Provided func is not None, in which case you need:
filter(None, seq) -> [x for x in seq if x]
There's no doubt in my mind that list comps are more general than filter,
and therefore more powerful, but for simple filters, I like filter().
I think filter would be more useful if it took an arbitrary number of
iterable arguments rather than just a single. The equivalent of:
filter(func, *seqs) -> [x for x in itertools.chain(*seqs) if func(x)]
although I suppose functional programming purists might object :)
More information about the Python-list