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


Groups > comp.lang.python > #42150 > unrolled thread

Re: list comprehension misbehaving

Started byTim Chase <python.list@tim.thechases.com>
First post2013-03-28 10:44 -0500
Last post2013-03-28 10:44 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#42150 — Re: list comprehension misbehaving

FromTim Chase <python.list@tim.thechases.com>
Date2013-03-28 10:44 -0500
SubjectRe: list comprehension misbehaving
Message-ID<mailman.3890.1364485398.2939.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web