Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!news2.euro.net!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.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'explicit': 0.07; 'subject:skip:c 10': 0.09; 'working:': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; '2.7': 0.14; '"a"': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'loop.': 0.16; 'wrote:': 0.18; 'all,': 0.19; '(in': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'refers': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'subject:list': 0.30; 'charset:us-ascii': 0.36; 'example,': 0.37; 'two': 0.37; 'list': 0.37; 'middle': 0.60; "you're": 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'taking': 0.65; 'dear': 0.65; 'results': 0.69; 'discover:': 0.84; 'received:50.22': 0.84; 'snapshot': 0.84 Date: Thu, 28 Mar 2013 10:44:51 -0500 From: Tim Chase To: Wolfgang Maier Subject: Re: list comprehension misbehaving In-Reply-To: References: X-Mailer: Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: python-list@python.org 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364485398 news.xs4all.nl 6898 [2001:888:2000:d::a6]:38471 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42150 On 2013-03-28 15:25, Wolfgang Maier wrote: > Dear all, with > a=list(range(1,11)) > > why (in Python 2.7 and 3.3) is this explicit for loop working: > for i in a[:-1]: > a.pop() and a As you discover: > Especially, since these two things *do* work as expected: > [a.pop() and a[:] for i in a[:-1]] it's because you're taking a snapshot copy of "a" in the middle of the loop. In your first example, if you change it to results = [] for i in a[:-1]: results.append(a.pop() and a) print results you get the same thing as your list comprehension because each item in "results" refers to the now-(mostly)empty "a". -tkc