Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Mar 17 10:52:57 CET 2007

En Sat, 17 Mar 2007 03:54:22 -0300, Milton Segura <syrax6l at hotmail.com>  

> Hello, I'm trying to exclude files from a list using the following code:
> for item in dirs:    if item.find('Software') > -1:         
> dirs.remove(item)    elif item.find('Images') > -1:         
> dirs.remove(item)
> For some reason.. it just won't exclude them.
> I'd like to know why and how to fix it.

Don't iterate over the list and alter it at the same time; the iterator  
gets confused.
Iterate over a copy:

for item in dirs[:]:
    if item.find(...)

or backwards:

for item in reversed(dirs):
    if item.find(...)

Gabriel Genellina

More information about the Python-list mailing list