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: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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.