[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Problems found while inspecting 2005->2007 debdiffs



Florent Rougon <f.rougon@free.fr> wrote:

> The problem was that I had something like:
>
>   for entry in <some list>:
>
>       <stuff that may remove an entry from <some list>>
>
> and in such a case, contrary to what I expected, the loop is not
> executed as many times as there were elements in <some list> before the
> loop: whenever elements are removed from <some list> inside the loop, we
> miss an iteration[1]. I would have expected that <some list> is
> evaluated once and for all at the beginning of the for loop, but things
> seem to work differently: the interpreter seems to remember that it is
> at index 0, then 1, then 2, etc. of <some list>, which causes entries in
> the original list to be skipped when we remove elements from
> <some list> whose indices are small enough.
>
> I fixed that by making a safe copy of the needed parts of <some list>
> before entering the loop.

To me, that sounds like a very peculiar feature (or rather bug) in
Python, since it is contrary to to what one expects.  Oh well, or not,
since I don't see an easy way to remove a particular entry from a
list/array in shell or perl.

Is it documented?

Regards, Frank
-- 
Dr. Frank Küster
Single Molecule Spectroscopy, Protein Folding @ Inst. f. Biochemie, Univ. Zürich
Debian Developer (teTeX/TeXLive)



Reply to: