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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'squares': 0.07; 'subject:skip:c 10': 0.07; 'cc:addr:python-list': 0.09; 'behave': 0.09; 'example:': 0.10; 'do,': 0.15; 'wed,': 0.15; 'blame': 0.16; 'clauses': 0.16; 'comp': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'range(20):': 0.16; 'something.': 0.16; 'unwound': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'nested': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'do.': 0.22; 'function,': 0.22; 'minor': 0.22; 'suppose': 0.22; 'code,': 0.23; 'header:In- Reply-To:1': 0.24; 'subject:list': 0.26; 'rest': 0.26; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'this.': 0.28; 'correct,': 0.29; 'that.': 0.30; 'too.': 0.30; 'code': 0.30; 'call.': 0.30; "i'd": 0.31; 'rules': 0.31; '[1]': 0.32; 'statement': 0.32; 'call,': 0.33; 'combination': 0.33; 'version:': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'skip:( 30': 0.35; 'could': 0.35; 'done': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'beginning': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'method': 0.37; "won't": 0.38; 'skip:o 20': 0.38; 'sure': 0.39; 'subject:with': 0.40; 'some': 0.40; 'matter': 0.63; 'skip:w 30': 0.64; "'for'": 0.84; 'chrisa': 0.84; 'expect.': 0.84; 'filtered': 0.84; 'to:none': 0.91; 'technically': 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=QIcQEpu37wyE7Z8w9NsaUJ1UOQAEcpB4gE8qFUFWG38=; b=huBzmiYTNcXE8vy8x34zf4VkOjA4xCqgeNjZ3nuEPEwaOPHySKsdYRDaZHcyBf5DyA gQc8k3sl+dhzZd7ZB63OwizDfT/eRr3rR5fWLDaO+4JcAABy23NkllqfzY9UA/yeEBjv Neew7XgbYAeMEYVUptBmKKQ9oq8cJ/opW2Vy9oBYlcSTZGMue6HDu5qiTWDM0uS4+cOm SyG6Je5LEU27NPqPVAwoKmBoRbezMIe2Lr9h0UcyLp3xi4SUkzwc10pSTAWHjSIAeXYq XkB7AktJEhU5xmn6x9+wRymxnOx0a+Nulg2lESGzaUc53OW7/7EOMtJA9NnPrFmU86Kx 5zAQ== MIME-Version: 1.0 X-Received: by 10.50.109.233 with SMTP id hv9mr4901097igb.92.1438768323586; Wed, 05 Aug 2015 02:52:03 -0700 (PDT) In-Reply-To: <87wpxacdnr.fsf@elektro.pacujo.net> References: <87wpxacdnr.fsf@elektro.pacujo.net> Date: Wed, 5 Aug 2015 19:52:03 +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: 75 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1438768326 news.xs4all.nl 2861 [2001:888:2000:d::a6]:35368 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95006 On Wed, Aug 5, 2015 at 7:01 PM, Marko Rauhamaa wrote: > Chris Angelico : > >> You can chain 'for' and 'if' clauses as much as you like, and they >> behave exactly the way you'd expect. > > How do you know what I'd expect? > > I wouldn't know what to expect myself. A list comprehension can always be unwound into statement form. [1] For example: squares = [n*n for n in range(10)] can be unwound into: squares = [] for n in range(10): squares.append(n*n) You simply take the expression at the beginning and put that into the append() method call, and then the rest become statements. Here's a filtered version: odd_squares = [n*n for n in range(20) if n%2] Which becomes: odd_squares = [] for n in range(20): if n%2: odd_squares.append(n*n) So what would you expect nested 'if' clauses to do? Well, they become nested 'if' statements: primes = [n for n in range(2,24) if n%2 if n%3] primes = [] for n in range(2,24): if n%2: if n%3: primes.append(n) What if we have multiple 'for' loops? Same thing! ways_to_get_seven = [(a,b) for a in range(1,7) for b in range(1,7) if a+b==7] ways_to_get_seven = [] for a in range(1,7): for b in range(1,7): if a+b==7: ways_to_get_seven.append((a,b)) No matter what combination of 'if' and 'for' you use, it can be unwound like this, and it'll always behave the same way. Not sure what to expect? Follow the simple rules of unwinding, and then read the code that way. Of course, if you don't know how to predict what the statement form will do, then you won't understand what the comprehension will do. But that's not the comprehension's fault. peculiar = [a*x*x+b*x+c for a in range(1,10) for b in range(2,30) for c in range(-3,5) if b*b-4*a*c>=0 if (-b+math.sqrt(b*b-4*a*c))/2/a>0 for x in (x*.01 for x in range(-500,500))] I suppose you could graph that. Or something. But that's just bad code, don't blame the list comp for that... ChrisA [1] To be technically correct, this unwinding should be done in a nested function, which you then call. There are some other minor technicalities too. But it's pretty much this.