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


Groups > comp.lang.python > #42155

Re: list comprehension misbehaving

From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: list comprehension misbehaving
Date 2013-03-28 15:52 +0000
References <loom.20130328T160914-263@post.gmane.org> <20130328104451.22bad9af@bigbox.christie.dr>
Newsgroups comp.lang.python
Message-ID <mailman.3893.1364485979.2939.python-list@python.org> (permalink)

Show all headers | View raw


Tim Chase <python.list <at> tim.thechases.com> writes:

> 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
> 
> 

Hi Tim, and thanks a lot for helping!
I got it:
in the comprehension case I'm only getting my results at the end,
when my list has been emptied!
Thanks, I got stuck with this, but now it's obvious.
Best,
Wolfgang

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


Thread

Re: list comprehension misbehaving Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-28 15:52 +0000

csiph-web