Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone05.am1.xlned.com!bcyclone05.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:skip:c 10': 0.07; 'cc:addr:python-list': 0.09; '"["': 0.09; 'pavel': 0.09; 'wed,': 0.15; '"]"': 0.16; '"for"': 0.16; '"if"': 0.16; '"in"': 0.16; '::=': 0.16; '[","]]': 0.16; '[(","': 0.16; 'clauses': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ifs': 0.16; 'list_display': 0.16; 'list_for': 0.16; 'list_if': 0.16; 'list_iter': 0.16; 'or_test': 0.16; 'target_list': 0.16; 'wrote:': 0.16; 'alternate': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'seems': 0.23; 'header :In-Reply-To:1': 0.24; 'subject:list': 0.26; 'message- id:@mail.gmail.com': 0.27; 'skip:[ 10': 0.31; 'received:google.com': 0.35; "isn't": 0.35; 'but': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'subject:with': 0.40; 'different': 0.63; "'and'": 0.84; "'for'": 0.84; 'chrisa': 0.84; 'clauses,': 0.84; 'to:none': 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=lIiVsYIBYeQiJIymzf3Fx3cyRR6zvbgTRu7M3f8zhLA=; b=Wm3YEi/psP6gjQce6VHVOonxuX0pYZoPitVfUzrelaeeQWswauT2GOjiXsWeSZijn4 AB414VYs4UXdYVyqmItYHzGiylZ7LeLItlsOvIVlcFzGNo5N+xJ4XLkj1cIUNfJ63Lux CTutloAIn0MaRda6AwmQNceZvAqiIBdLHmR+rt9nBNzHqw2U74nqvj+VSuhdjA0+67d6 11oDjYVs5upaPOTRpsfT6ov0mhkLdTEE14MIQk4RJoXE2WAq8c4n1cPDconjksVMqFML wPNTGX0E6Bas23z2guXZQJmJ68L7Lsu8I5U3PfikTx+XETjFyrQ/Z13qKdO03f2eg8Af hR9A== MIME-Version: 1.0 X-Received: by 10.50.60.68 with SMTP id f4mr9355947igr.94.1438759205584; Wed, 05 Aug 2015 00:20:05 -0700 (PDT) In-Reply-To: <631b480d-497a-4451-9577-2788f9d4c85b@googlegroups.com> References: <631b480d-497a-4451-9577-2788f9d4c85b@googlegroups.com> Date: Wed, 5 Aug 2015 17:20:05 +1000 Subject: Re: GOTCHA with list comprehension From: Chris Angelico Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1438759213 news.xs4all.nl 2911 [2001:888:2000:d::a6]:37359 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 4079 X-Received-Body-CRC: 2497873584 Xref: csiph.com comp.lang.python:95001 On Wed, Aug 5, 2015 at 5:03 PM, Pavel S wrote: > It seems this is allowed by the grammar: > > list_display ::= "[" [expression_list | list_comprehension] "]" > list_comprehension ::= expression list_for > list_for ::= "for" target_list "in" old_expression_list [list_iter] > old_expression_list ::= old_expression [("," old_expression)+ [","]] > old_expression ::= or_test | old_lambda_expr > list_iter ::= list_for | list_if > list_if ::= "if" old_expression [list_iter] > > So chaining multiple ifs is fine: > > [ i for i in range(10) if True if True if True if True ] Yep. A chain of 'if' clauses isn't materially different from a single 'if' with a bunch of 'and' checks, but if you alternate 'if' and 'for' clauses, you get a comprehension that conditionally nests its iterations. ChrisA