Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; 'subject:Python': 0.06; 'comment,': 0.09; 'subject:language': 0.09; 'cc:addr:python-list': 0.11; '(1,': 0.16; '1),': 0.16; '2),': 0.16; '24,': 0.16; '3)]': 0.16; 'brackets,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterable,': 0.16; 'length,': 0.16; 'subject: \n ': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'do.': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'purposes': 0.26; 'query': 0.26; 'header:In-Reply-To:1': 0.27; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'probably': 0.32; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'returning': 0.36; 'list': 0.37; 'easily': 0.37; 'pm,': 0.38; 'changed': 0.39; 'range': 0.61; 'dont': 0.67; 'mar': 0.68; 'square': 0.74; 'subject:this': 0.83; '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=9CyGPuXaNyTiAPkPzDu4QIfouj3Je0+jsA1yNvcW+0k=; b=yKvFDwYq2JlRaHaHOfCU4vaNwdw+lxGg03N9Y1bW0c093GWRFLBfIJ4+Fawe/lueie ah0a1nsces7AtpWM2Ruwcc0xCQqVnSOCnUEx4bDwwsAvLGWtTtDdAld96Mmaqts2mH2r BhhUkFXhOcHyfRMansb58lkrSU+3G6/mHPwnaxaq/MKG0xEAAkxZtrCRrIASU+1nDQZm u3qJZRvOfn6xArk/5y0PxD8GZn2gYHrLvFuY+6BZO7j26eBKU3gSzl4Wvq5VqSz7oyMF tpUpHS8QJ5s5K1QcMAQW1GUdQl3vVyTlgzByp8VvTtKsNZbsqr8c2FpLKR/uhDYLSHGp 8tzQ== MIME-Version: 1.0 X-Received: by 10.66.129.133 with SMTP id nw5mr68300165pab.98.1395637491337; Sun, 23 Mar 2014 22:04:51 -0700 (PDT) In-Reply-To: <8a786235-3030-483c-8153-b3b420510782@googlegroups.com> References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> <9edb4ea0-5faf-4369-8021-48afa9800a34@googlegroups.com> <8a786235-3030-483c-8153-b3b420510782@googlegroups.com> Date: Mon, 24 Mar 2014 16:04:51 +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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395637499 news.xs4all.nl 2891 [2001:888:2000:d::a6]:36156 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68850 On Mon, Mar 24, 2014 at 3:14 PM, Rustom Mody wrote: > Neat! So I play around... Change it to > [(x,y) for x in range(1,10000) for y in range(1,10000)] > and I dont have an answer but a thrashing machine!! (*) Yes, because you used square brackets, which means that the list has to be fully realized. As you comment, range changed from returning a list to returning an iterable, and this action is similarly cheap: >>> ((x,y) for x in range(1,10000) for y in range(1,10000)) at 0x7f53ed61b360> You can take a few elements from that cheaply: >>> [next(_),next(_),next(_)] [(1, 1), (1, 2), (1, 3)] If you like thinking in "lazy lists", you can probably think just as easily with generators; you can't pull up arbitrary elements from it, or query its length, but for many purposes a generator will do. ChrisA