Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicit': 0.07; '[1,': 0.09; 'append': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:c 10': 0.09; 'working:': 0.09; 'python': 0.11; '2.7': 0.14; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; 'all,': 0.19; '>>>': 0.22; '(in': 0.22; 'header:User-Agent:1': 0.23; 'equivalent': 0.26; 'header:X-Complaints-To:1': 0.27; '[1]': 0.29; '???': 0.30; 'subject:list': 0.30; 'but': 0.35; 'two': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'no.': 0.61; "you'll": 0.62; 'dear': 0.65; 'introduce': 0.78 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: list comprehension misbehaving Date: Thu, 28 Mar 2013 16:48:03 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a4b4.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364485670 news.xs4all.nl 6988 [2001:888:2000:d::a6]:41806 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42152 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 > > giving: > [1, 2, 3, 4, 5, 6, 7, 8, 9] > [1, 2, 3, 4, 5, 6, 7, 8] > [1, 2, 3, 4, 5, 6, 7] > [1, 2, 3, 4, 5, 6] > [1, 2, 3, 4, 5] > [1, 2, 3, 4] > [1, 2, 3] > [1, 2] > [1] No. Introduce a result list, and you'll see that you append the *same* list to the result nine times: >>> a = range(1, 11) >>> result = [] >>> for i in a[:-1]: ... result.append(a.pop() and a) ... >>> result [[1], [1], [1], [1], [1], [1], [1], [1], [1]] > but the equivalent comprehension failing: > [a.pop() and a for i in a[:-1]] > > giving: > [[1], [1], [1], [1], [1], [1], [1], [1], [1]] > > ??? > Especially, since these two things *do* work as expected: > [a.pop() and a[:] for i in a[:-1]] > [a.pop() and print(a) for i in a[:-1]] # Python 3 only So you already know the solution to your problem...