need insight...

Tim Peters tim_one at email.msn.com
Mon Jun 26 19:00:36 EDT 2000


[posted and mailed]

[David Broadwell]
> ... statement the  'for item in directorylist:' seems to be skipping
> over entries in it's list for no apparent reason... (note: what is
> skipps changes if the list it is given chages ...

>         ...
>         for thing in subdir:
>             ...
>             if ...:
>                ...
>             else:
>                ...
>                subdir.remove(thing)
> ...

You're modifying subdir *while* you're iterating over it.  You can read the
full story on why you shouldn't do that in the Language Reference Manual
(see the section about "for" loops).  Try

    for thing in subdir[:]:

instead.  That is, iterate over a *copy* of subdir.  Then modifications to
the original won't interfere with the iteration (which is over a copy).

you-can't-spend-money-and-save-it-at-the-same-time-either<wink>-ly y'rs
    - tim






More information about the Python-list mailing list