List problem

Erik Max Francis max at
Sat Oct 30 00:23:22 CEST 2004

Cliff Wells wrote:

> Another approach (that doesn't require creating a copy of the list) is
> to iterate backwards over it:

Another way would be to not iterate over the list per se, but rather use
indices into the list and increment them when you delete an item, or
don't when you don't, thereby going over the entire list:

	i = 0
	while i < len(aList):
	    if dontWant(aList[i]):
	        del aList[i]
	        i += 1

 __ Erik Max Francis && max at &&
/  \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ I am not afraid / To be a lone Bohemian
    -- Lamya

More information about the Python-list mailing list