Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108207

Re: Pylint prefers list comprehension over filter...

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Stephen Hansen <me@ixokai.io>
Newsgroups comp.lang.python
Subject Re: Pylint prefers list comprehension over filter...
Date Thu, 05 May 2016 18:37:11 -0700
Lines 23
Message-ID <mailman.422.1462498634.32212.python-list@python.org> (permalink)
References <572BF2BF.6000000@icloud.com> <1462498631.232041.599637409.25D91C08@webmail.messagingengine.com>
Mime-Version 1.0
Content-Type text/plain
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de scMDOMs7d+Q0TNRq8dpYlQNUJyP46fRSzJ+Uq1czrLEQ==
Return-Path <me@ixokai.io>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'discard': 0.05; 'needed,': 0.05; 'great.': 0.07; 'subject:skip:c 10': 0.07; 'received:internal': 0.09; 'thu,': 0.15; 'list*': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'pulls': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'skips': 0.16; 'wrote:': 0.16; 'string': 0.17; "shouldn't": 0.18; 'first,': 0.20; 'stephen': 0.22; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'said,': 0.27; 'correct': 0.28; 'looks': 0.29; 'creating': 0.30; "i'd": 0.31; 'though,': 0.32; 'options': 0.33; 'case,': 0.34; 'list': 0.34; 'filter': 0.35; 'returning': 0.35; 'list,': 0.36; 'instead': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'difference': 0.38; 'received:66': 0.38; 'building': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'header:Message-Id:1': 0.61; 'default': 0.61; 'believe,': 0.84; 'subject:over': 0.84; 'good,': 0.93
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d=ixokai.io; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=PrZFuzs1OBNAoRg4CeE7IkaUiqg=; b=T4cAYy fQoT2DKkzFTzmrYj6La++8hAlyuvp1Af+jWby9moj4XewxgnMBQsewfvJ6HVPYQF Pi5G26PYHpC8iWvD7+LgEyJZh3ibq9y2M9/l+y0H1YZ0w32QchsnmmQz6WgezeSY nY78a7/nNHHy1X9JVQDr1/xV/1vh+GihvkylY=
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=PrZFuzs1OBNAoRg 4CeE7IkaUiqg=; b=qrReQsKiDU7yZsXDME8BAAOdJ4pXKumEIu6q/VoXZUOdTRT MCPGaPMXeUE7AKfDEkR9bQV0+KBEHmj1fTasxG9868nQo6zdnQjx+7ylFUYjCqmu OucWsV/YCZvs2wDiQ0z6FkkfsJfl/cJ18FKFcJKo9ulO8eSQOEPy+dN+VYws=
X-Sasl-Enc 0Kk184ldPr0vok2q+5yy8sYmx7E3235hPjYCVgKGSuSK 1462498631
X-Mailer MessagingEngine.com Webmail Interface - ajax-140377c4
In-Reply-To <572BF2BF.6000000@icloud.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <1462498631.232041.599637409.25D91C08@webmail.messagingengine.com>
X-Mailman-Original-References <572BF2BF.6000000@icloud.com>
Xref csiph.com comp.lang.python:108207

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Pylint prefers list comprehension over filter... Stephen Hansen <me@ixokai.io> - 2016-05-05 18:37 -0700
  Re: Pylint prefers list comprehension over filter... Dan Sommers <dan@tombstonezero.net> - 2016-05-06 02:46 +0000
    Re: Pylint prefers list comprehension over filter... Chris Angelico <rosuav@gmail.com> - 2016-05-06 12:55 +1000
    Re: Pylint prefers list comprehension over filter... Stephen Hansen <me+python@ixokai.io> - 2016-05-05 19:57 -0700
      Re: Pylint prefers list comprehension over filter... Steven D'Aprano <steve@pearwood.info> - 2016-05-06 22:42 +1000
    Re: Pylint prefers list comprehension over filter... Dan Sommers <dan@tombstonezero.net> - 2016-05-06 03:07 +0000
      Re: Pylint prefers list comprehension over filter... Chris Angelico <rosuav@gmail.com> - 2016-05-06 13:18 +1000
    Re: Pylint prefers list comprehension over filter... Christopher Reimer <christopher_reimer@icloud.com> - 2016-05-07 12:37 -0700

csiph-web