os.walk bug?

Terry Reedy tjreedy at udel.edu
Sat Dec 18 15:45:13 EST 2004


"Alex Martelli" <aleaxit at yahoo.com> wrote in message 
news:1goxmng.1edap1z1cok1cnN%aleaxit at yahoo.com...
> for item in alist:
>    if isnasty(item):
>        alist.remove(item)
>
> changing the header to ``in alist[:]:'' or ``in list(alist):'' probably
> makes this code work, but it still can't make it GOOD... good would be:
>
> alist[:] = [item for item in alist if not isnasty(item)]

Given that the OP has verified that replacing alist with list(alist) in the 
for statement solves his problems, I agree that the listcomp is even nicer 
in that it avoids the bug trap, the over-copying of the entire list, the 
alternative of iterating in reverse, and the n**2 behavior of multiple 
correct deletes from a long list.

Terry J. Reedy






More information about the Python-list mailing list