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


Groups > comp.lang.python > #94998

Re: GOTCHA with list comprehension

Path csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'true,': 0.04; '"no"': 0.07; 'false,': 0.07; 'none)': 0.07; 'subject:skip:c 10': 0.07; 'incorrect': 0.09; 'pavel': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; "'o',": 0.16; '2.6:': 0.16; "['f',": 0.16; 'boolean': 0.16; 'evaluates': 0.16; 'expression.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'typo': 0.16; 'wrote:': 0.16; 'detect': 0.18; 'language': 0.19; '>>>': 0.20; '(not': 0.20; 'constant': 0.22; 'visible': 0.22; 'feature': 0.24; 'wondering': 0.25; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X -Complaints-To:1': 0.26; 'values': 0.28; '"yes"': 0.29; 'accepts': 0.29; 'context,': 0.29; 'branch': 0.30; 'values.': 0.33; 'list': 0.34; 'item': 0.35; 'but': 0.36; "wasn't": 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'doing': 0.38; 'hi,': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'received:de': 0.40; 'detail.': 0.66; 'was:': 0.66; 'results': 0.66; 'finally': 0.70; 'look.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: GOTCHA with list comprehension
Date Wed, 05 Aug 2015 09:04:51 +0200
Organization None
References <ebf0a31e-1021-41d2-affb-8318838a464b@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd8fce.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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>
Newsgroups comp.lang.python
Message-ID <mailman.1230.1438758310.3674.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1438758310 news.xs4all.nl 2940 [2001:888:2000:d::a6]:54160
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94998

Show key headers only | View raw


Pavel S wrote:

> Hi,
> 
> I recently found interesting GOTCHA while doing list comprehension in
> python 2.6:
> 
>>>> values = ( True, False, 1, 2, 3, None )
>>>> [ value for value in values if value if not None ]
> [True, 1, 2, 3]
> 
> I was wondering why this list comprehension returns incorrect results and
> finally found a typo in the condition. The typo wasn't visible at the
> first look.
> 
> My intention was: if value is not None
> But I wrote: if value if not None
> 
> Is that a language feature of list comprehension that it accepts
> conditions like: if A if B if C if D ...?

I think it's just that a condition may be a constant expression. Python 
evaluates (not None) for every item in values. Other variants:

>>> if 42:
...     print("branch always taken")
... 
branch always taken
>>> always_yes = "yes" if True else "no"
>>> always_yes
'yes'
>>> [c for c in "foo" if "true in a boolean context"]
['f', 'o', 'o']

An optimizer might detect that (not None) is always True in a boolean 
context, but that would be an implementation detail.


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


Thread

GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-04 23:48 -0700
  Re: GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-05 00:03 -0700
    Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 17:20 +1000
  Re: GOTCHA with list comprehension Peter Otten <__peter__@web.de> - 2015-08-05 09:04 +0200
  Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 17:05 +1000
    Re: GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-05 00:10 -0700
      Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 17:21 +1000
        Re: GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-05 00:33 -0700
    Re: GOTCHA with list comprehension Marko Rauhamaa <marko@pacujo.net> - 2015-08-05 12:01 +0300
      Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 19:52 +1000
        Re: GOTCHA with list comprehension Marko Rauhamaa <marko@pacujo.net> - 2015-08-05 14:10 +0300
      Re: GOTCHA with list comprehension Saran Ahluwalia <ahlusar.ahluwalia@gmail.com> - 2015-08-07 09:08 -0400
  Re: GOTCHA with list comprehension Laura Creighton <lac@openend.se> - 2015-08-06 06:39 +0200
  Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-06 14:44 +1000

csiph-web