Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42150

Re: list comprehension misbehaving

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 <python.list@tim.thechases.com>
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 <python.list@tim.thechases.com>
To Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: list comprehension misbehaving
In-Reply-To <loom.20130328T160914-263@post.gmane.org>
References <loom.20130328T160914-263@post.gmane.org>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3890.1364485398.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: list comprehension misbehaving Tim Chase <python.list@tim.thechases.com> - 2013-03-28 10:44 -0500

csiph-web