Pylint prefers list comprehension over filter...

Chris Angelico rosuav at gmail.com
Thu May 5 22:55:05 EDT 2016


On Fri, May 6, 2016 at 12:46 PM, Dan Sommers <dan at tombstonezero.net> wrote:
> filter used to build a list, but now it doesn't (where "used to" means
> Python 2.7 and "now" means Python 3.5; I'm too lazy to track down the
> exact point(s) at which it changed):
>
>     Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
>     [GCC 5.3.1 20160409] on linux2
>     Type "help", "copyright", "credits" or "license" for more information.
>     >>> filter(lambda x:x+1, [1, 2, 3, 4])
>     [1, 2, 3, 4]
>
>     Python 3.5.1+ (default, Apr 17 2016, 16:14:06)
>     [GCC 5.3.1 20160409] on linux
>     Type "help", "copyright", "credits" or "license" for more information.
>     >>> filter(lambda x:x+1, [1, 2, 3, 4])
>     <filter object at 0x7f26a9ef3320>

Most of these kinds of changes happened in 3.0, where
backward-incompatible changes were accepted. A whole bunch of things
stopped returning lists and started returning lazy iterables - range,
filter/map, dict.keys(), etc - because most of the time, they're
iterated over once and then dropped.

ChrisA



More information about the Python-list mailing list