list.pop([i]) and list.remove(x) w/ for loops

Skip Montanaro skip at mojam.com
Thu Jun 29 10:54:00 EDT 2000


    >> I was getting some unexpected behavior when using list.remove(x),
    >> wondering if someone can tell me what's going on.

    Moshe> Short answer: never ever modify a list you're iterating on. Make
    Moshe> a copy first.

In situations where copying the list is impractical, you can iterate over
the list in reverse order:

    for i in range(len(olist)-1,-1,-1):
	if o.type == a:
	    del olist[i]

-- 
Skip Montanaro, skip at mojam.com, http://www.mojam.com/, http://www.musi-cal.com/
On Phil Jackson's ability to manage multiple Lakers superstars, Shaquille
O'Neal said: "He's got the rings.  I listen to the man with the rings."




More information about the Python-list mailing list