Pylint prefers list comprehension over filter...

Steven D'Aprano steve at pearwood.info
Fri May 6 08:42:59 EDT 2016


On Fri, 6 May 2016 12:57 pm, Stephen Hansen wrote:

> On Thu, May 5, 2016, at 07:46 PM, Dan Sommers wrote:
>> 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):
> 
> Oh, didn't know that. Then again the OP was converting the output of
> filter *into* a list, which wasted a list either way.


In Python 2.7, run `from future_builtins import *` to get most of the Python
3 behaviour:


py> filter(None, [])
[]
py> from future_builtins import *
py> filter(None, [])
<itertools.ifilter object at 0xb7ee3e8c>



-- 
Steven




More information about the Python-list mailing list