Pylint prefers list comprehension over filter...
Stephen Hansen
me at ixokai.io
Thu May 5 21:37:11 EDT 2016
On Thu, May 5, 2016, at 06:26 PM, Christopher Reimer wrote:
> Which is one is correct (Pythonic)? Or does it matter?
First, pylint is somewhat opinionated, and its default options shouldn't
be taken as gospel. There's no correct: filter is fine.
That said, the general consensus is, I believe, that list comprehensions
are good, and using them is great.
In your case, though, I would not use a list comprehension. I'd use a
generator comprehension. It looks almost identical:
''.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, and join pulls items out of it one
at a time as it uses them. No needlessly creating a list only to use it
and discard it.
--
Stephen Hansen
m e @ i x o k a i . i o
More information about the Python-list
mailing list