Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #94999
| Path | csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.007 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'true,': 0.04; 'false,': 0.07; 'subject:skip:c 10': 0.07; 'cc:addr:python-list': 0.09; 'behave': 0.09; 'incorrect': 0.09; 'none"': 0.09; 'pavel': 0.09; 'python': 0.10; '2.7': 0.13; 'wed,': 0.15; 'result.': 0.15; '2.6:': 0.16; 'clauses': 0.16; 'false),': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hint': 0.16; 'typo': 0.16; 'wrote:': 0.16; 'language': 0.19; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'visible': 0.22; 'code,': 0.23; 'header:In-Reply-To:1': 0.24; 'feature': 0.24; 'wondering': 0.25; 'subject:list': 0.26; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'values': 0.28; 'accepts': 0.29; 'branch': 0.30; 'certainly': 0.30; "can't": 0.32; 'possibly': 0.32; 'though,': 0.32; '(for': 0.34; 'that,': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'but': 0.36; 'possible': 0.36; "wasn't": 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'release': 0.37; 'doing': 0.38; 'version': 0.38; 'stuff': 0.38; 'hi,': 0.38; 'why': 0.39; 'test': 0.39; 'subject:with': 0.40; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'back': 0.62; 'improved': 0.63; 'is.': 0.63; 'grab': 0.64; 'was:': 0.66; 'results': 0.66; 'finally': 0.70; 'yourself': 0.73; "'for'": 0.84; '2.7.': 0.84; 'chrisa': 0.84; 'expect.': 0.84; 'to:none': 0.91; 'look.': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Q/Yp8okHzUvmuTen3JQup5fxKGPSQi+FYrQQbdDlrvA=; b=PHvbmbTlT2i5cgrvaOSX7ShfCZ1v1y2WPv+FCPfI8m6jPNuJbDBbVOm1yMbyFjq9Oq a6UtpJTHTCE8Wl1SLqB1jmefhS4azUQlvUW1FbOI1yuOH2zNfq2X0pxH+73pukkSAvn2 BnX0/lJ0b0swRlv50jjkqZkk1yY4LPpgGcwgAFRYrn+sXqb8nZXWzVu8XzYUXO8VX9m6 t7j9n5jtkCnrsq3ouZZH/OlF1HN0TPoDVTu0P7YPoMCHrGA28I099KQLDuM0XYTKj388 h/CNu69EMB5C9hHUKa/Dy1sm2bYFyf7IGs8IZgVucL2ZJMEC27xCWNHs+BhNtnkVKygm sOBg== |
| MIME-Version | 1.0 |
| X-Received | by 10.50.109.233 with SMTP id hv9mr4291873igb.92.1438758350020; Wed, 05 Aug 2015 00:05:50 -0700 (PDT) |
| In-Reply-To | <ebf0a31e-1021-41d2-affb-8318838a464b@googlegroups.com> |
| References | <ebf0a31e-1021-41d2-affb-8318838a464b@googlegroups.com> |
| Date | Wed, 5 Aug 2015 17:05:49 +1000 |
| Subject | Re: GOTCHA with list comprehension |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| 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.1231.1438758358.3674.python-list@python.org> (permalink) |
| Lines | 31 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1438758358 news.xs4all.nl 2826 [2001:888:2000:d::a6]:55161 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:94999 |
Show key headers only | View raw
On Wed, Aug 5, 2015 at 4:48 PM, Pavel S <pavel@schon.cz> 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 ...?
It certainly is. You can chain 'for' and 'if' clauses as much as you
like, and they behave exactly the way you'd expect. You might possibly
get a warning from a linter with your code, though, as it has an
always-true condition ("if not None" can never be false), so it's
possible something might hint at what's going on; but other than that,
all you can do is test stuff and see if it's giving the right result.
Incidentally, why Python 2.6? Python 2.7 has been out for a pretty
long time now, and if you can't move to version 3.x, I would at least
recommend using 2.7. Since the release of 2.6.9 back before Frozen
came out, that branch has been completely unmaintained. Grab yourself
a 2.7 and take advantage of some neat new features (for old values of
"new"), and improved compatibility with 3.x.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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