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


Groups > comp.lang.python > #42150

Re: list comprehension misbehaving

Date 2013-03-28 10:44 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: list comprehension misbehaving
References <loom.20130328T160914-263@post.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.3890.1364485398.2939.python-list@python.org> (permalink)

Show all headers | 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