Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'languages,': 0.04; 'syntax': 0.04; 'beginner': 0.05; 'subject:Python': 0.06; 'explicit': 0.07; 'modified': 0.07; 'squares': 0.07; 'subject:language': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'language.': 0.14; '"classic"': 0.16; '"for"': 0.16; '(int': 0.16; '*real*': 0.16; 'business?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'loop.': 0.16; 'mean,': 0.16; 'once.': 0.16; 'readable': 0.16; 'subject: \n ': 0.16; 'utterly': 0.16; 'whom?': 0.16; 'language': 0.16; 'wrote:': 0.18; "python's": 0.19; 'code,': 0.22; 'example': 0.22; 'programming': 0.22; 'cc:addr:python.org': 0.22; 'either.': 0.24; 'filtering': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'point': 0.28; 'am,': 0.29; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; '"do': 0.31; '(maybe': 0.31; '25,': 0.31; "d'aprano": 0.31; 'obscure': 0.31; 'pascal': 0.31; 'steven': 0.31; 'probably': 0.32; 'basic': 0.35; 'something': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'keyword': 0.36; 'done': 0.36; 'list': 0.37; 'does': 0.39; "couldn't": 0.39; 'though,': 0.39; 'use.': 0.39; 'called': 0.40; 'how': 0.40; 'even': 0.60; 'truly': 0.60; 'first': 0.61; 'times': 0.62; 'more': 0.64; 'situation': 0.65; 'mar': 0.68; 'miss': 0.74; 'subject:this': 0.83; 'actually,': 0.84; 'confusion.': 0.84; 'to:none': 0.92; 'imagine': 0.93 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=LNUE95FvggTvrLT4I6yfBacCf+nfRaDqL2RInLtrMfE=; b=FGBK4rdRUnxnK/1MjF31ecorPjNeG+kDULF51Ta52B6LquTd7Xf0yefjdahZ8cTme1 PlSwhp0GuzoGtjxuBdS5eAYZYz8yyuBEECBtk9IoRr3kshSyr3drQdw+laOQTyIN5HXE +oDk/MJnL20B+L/c6WS7M64YUrlHnze4GhL4yT8Tb+t2I9WcEkrUW+zftRmxQEBlCPnu tphK7rvZoJYB1Nu9ViiMsnBkGZsr5/7c5TKx8bhVI4r4g3sOPe563G+IFBYfFnhl0zOS e30U59Ed3A0pRpXZs2s8hBogGRc9ycQ1GKcNgP1vvTE8Y6ep06FBcar0zs2tvrrB5G9i ovQQ== MIME-Version: 1.0 X-Received: by 10.68.235.6 with SMTP id ui6mr71824510pbc.45.1395688367383; Mon, 24 Mar 2014 12:12:47 -0700 (PDT) In-Reply-To: <53303b8a$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> <532d5bd9$0$29994$c3e8da3$5496439d@news.astraweb.com> <87bnwv2a5v.fsf@elektro.pacujo.net> <53303b8a$0$29994$c3e8da3$5496439d@news.astraweb.com> Date: Tue, 25 Mar 2014 06:12:47 +1100 Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395688376 news.xs4all.nl 2971 [2001:888:2000:d::a6]:42682 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68893 On Tue, Mar 25, 2014 at 1:04 AM, Steven D'Aprano wrote: >> But which of these is truly more readable? >> >> squares = [] >> for n in range(30): >> squares.append(n * n) >> >> squares = [n * n for n in range(30)] > > Readable for whom? > > List comprehension syntax is often completely obscure to beginners. A > beginner would say that the explicit for-loop is more readable. > > Actually, a *real* beginner, whose main programming experience before > Python was Pascal, would probably even say that the first example was an > unreadable mess. What's range(30)? What's this ".append" business? What > does [] mean? I know this because I was this beginner, once. The first > few times I tried reading Python code, I couldn't make head or tail of > it. "for" I recognised, because it was the same keyword as Pascal and > Hypertalk use. Pretty much everything else might as well have been > Bulgarian. Actually, that's a very good point. Python's for loop is more often called a foreach loop in other languages, and Python completely lacks any concept of a "classic" iteration-over-integer for loop. That is a point of confusion. However, that's going to come up on both branches, so it's not really a mark against either. Incidentally, I've often modified my loop counter, in C or REXX or any other language. About the only situation where I actually miss it in Python, though, is iterating over a list and mutating the list on the way through; and even that can often be done in other ways (maybe a list comp, filtering out some of the elements?). It's amazing how something can be so utterly fundamental (I mean, come ON! Who can imagine a language with no equivalent of the basic "do i=1 to 10" (REXX) or "for (int i=0;i<10;++i)" (C++) loop???) and yet so dispensable. ChrisA