list comprehension misbehaving

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Thu Mar 28 11:52:29 EDT 2013


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





More information about the Python-list mailing list