Pylint prefers list comprehension over filter...

Dan Sommers dan at tombstonezero.net
Thu May 5 22:46:22 EDT 2016


On Thu, 05 May 2016 18:37:11 -0700, Stephen Hansen wrote:

>     ''.join(x for x in string if x.isupper())

> The difference is, both filter and your list comprehension *build a
> list* which is not needed, and wasteful. The above skips building a
> list, instead returning a generator ...

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>



More information about the Python-list mailing list