Path: csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'column': 0.07; 'nested': 0.07; 'subject:two': 0.07; 'expression:': 0.09; 'matched': 0.09; 'cc:addr:python-list': 0.11; 'mostly': 0.14; '"for"': 0.16; '(there': 0.16; '1400': 0.16; '23,': 0.16; 'crude': 0.16; 'doing,': 0.16; 'expressions)': 0.16; 'ivan': 0.16; 'nesting': 0.16; 'otoh,': 0.16; 'pity': 0.16; 'roy': 0.16; 'sure.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'work,': 0.20; 'cc:addr:python.org': 0.22; 'rapidly': 0.24; 'skip': 0.24; 'stick': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'leave': 0.29; 'am,': 0.29; 'generally': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'regular': 0.32; 'subject:with': 0.35; 'received:google.com': 0.35; 'next': 0.36; 'experience,': 0.37; 'two': 0.37; 'list': 0.37; 'nov': 0.38; 'subject:needed': 0.38; 'needed': 0.38; 'enough': 0.39; 'full': 0.61; 'subject:skip:C 10': 0.61; 'simply': 0.61; 'such': 0.63; 'more': 0.64; 'smith': 0.68 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:to :cc:content-type; bh=T5AbK3uAXA7tMV+DCS2ry3ctzEX3TMR5yLu23plx99M=; b=U6EouqMRNFOQhy34+rKjisZI/WWbdD5K5OGCf+ku5bQLo7937w80Dp/Pw0h1Bh2Lo7 iUqKowAUL96WqiHOOMukgmRi411vKE5FA/whnoKvcm+qJDtMlo02BdEUHAffulV1ufAU xLsYY6Uv75udc2WHrjZMPuukBl2yNqvskCO9GMqyWylTNWcR5U8hRX/4yRzJkgGpJudF UqR8tQK1Iy0z0H5oWQLIqFqlvzM9vi+gJZ/0Ox+78HgwefyyocgdsEwGwd6OL02qiLSM pSuyA5uM65qmWSkbKCMsuLpdOgNNEhskqucKLQYY1sMbdWRm8D0ab5nnde0vztJZKWRJ Kgaw== MIME-Version: 1.0 X-Received: by 10.220.195.196 with SMTP id ed4mr10119756vcb.65.1416761108168; Sun, 23 Nov 2014 08:45:08 -0800 (PST) In-Reply-To: References: <002501d00733$6995dce0$3cc196a0$@gmail.com> Date: Sun, 23 Nov 2014 10:45:08 -0600 Subject: Re: Comprehension with two variables - explanation needed From: Skip Montanaro To: Roy Smith Content-Type: text/plain; charset=UTF-8 Cc: Python 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416761116 news.xs4all.nl 2974 [2001:888:2000:d::a6]:57023 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81387 On Sun, Nov 23, 2014 at 9:57 AM, Roy Smith wrote: > > If it was complicated enough that you needed to loopify it to > understand what it's doing, have pity on the next person who has to > maintain your code and leave it as a loop Well, sure. I was mostly trying to give Ivan a path out of the weeds. Poking through the code I'm involved with at work, using a crude measuring stick (a couple regular expressions) I found over 1400 one line list comprehensions, those which matched this regular expression: \[.* for .*\] OTOH, I only found 14 with two "for" keywords: \[.* for .* for .*\] (There may well have been more nested loop list comprehensions, simply because it's hard to cram the full listcomp into 80 columns, my self-imposed column limit.) In my experience, list comprehensions rapidly get unwieldy once you get to nesting them. I generally unravel such items into a nested set of for loops. Skip