Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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; 'class,': 0.07; 'nested': 0.07; 'subject:two': 0.07; 'correct,': 0.09; 'cc:addr:python- list': 0.11; 'creates': 0.14; 'books': 0.15; '24,': 0.16; 'brackets': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'innermost': 0.16; 'ivan': 0.16; 'through,': 0.16; 'wrote:': 0.18; 'starts': 0.20; 'example': 0.22; 'cc:addr:python.org': 0.22; 'initial': 0.24; 'math': 0.24; 'mon,': 0.24; 'looks': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'class': 0.32; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'right?': 0.36; 'nov': 0.38; 'subject:needed': 0.38; 'subject:skip:C 10': 0.61; 'entire': 0.61; "you're": 0.61; 'back': 0.62; 'fact,': 0.69; 'analysis': 0.75; 'maths': 0.84; 'to:none': 0.92 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=QNZUBkjZEvfV4SVmQPMVzDwRERdGoSTdKj0AUGY8MXs=; b=GytAV1YtmKO09ZBK3v3zwnhZwcRGXbLFrzc5CFY3u/foBfIhFq5zgcqKFCQiirpnse q18DvYm9bhKfbJR1sIPERC8KnFhgh5MWKB76LvlU1I/daKzQr0sPykcS7x+7Y9YQ9f4F 7X4/SYhQovw6GksOmonj7vc/hveYubnxdzAT31Br2Z9m8pJb+uJCwjTLe9MgJbe2NI60 Guat6vg2rAIzV/7q2Tq4KeRxra0Y+9Q9SBJiTmSZG0AsGjU7vfd+MTBshW2hJT5o5MZq CKtCrNYaGgoQMl0G9t/5Cjof0Q2fzylp0oft7+f1WOJMC7kUfUqr5MldsZTEIX/uH/i1 Ba/Q== MIME-Version: 1.0 X-Received: by 10.107.134.39 with SMTP id i39mr13741872iod.53.1416757336618; Sun, 23 Nov 2014 07:42:16 -0800 (PST) In-Reply-To: <002501d00733$6995dce0$3cc196a0$@gmail.com> References: <002501d00733$6995dce0$3cc196a0$@gmail.com> Date: Mon, 24 Nov 2014 02:42:16 +1100 Subject: Re: Comprehension with two variables - explanation needed 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.15 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: 1416757339 news.xs4all.nl 2902 [2001:888:2000:d::a6]:52547 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81376 On Mon, Nov 24, 2014 at 2:37 AM, Ivan Evstegneev wrote: > As I know from books and googling, the comps main idea looks approximately > like this: > > [target <--main loop<--nested loop/s (and maybe some conditions)] Am I > right? > > But your code looks somehow inverted to me o_0 > > Like: > > [[target with nested loop] <--- main loop with initial values for target] Your analysis is correct, and it's because of the bracketing. Just as you learned back in maths class (or math class, if you're that sort), you start from the innermost brackets and work your way out. In fact, the nested example starts with the main loop, and for each value it goes through, it creates the entire inner comprehension. ChrisA