remove elements incrementally from a list

superpollo utente at esempio.net
Wed May 19 07:00:01 EDT 2010


Javier Montoya ha scritto:
> Dear all,
> 
> I've a list of float numbers and I would like to delete incrementally
> a set of elements in a given range of indexes, sth. like:
> 
> for j in range(beginIndex, endIndex+1):
>    print ("remove [%d] => val: %g" % (j, myList[j]))
>    del myList[j]
> 
> However, since I'm iterating over the same list, the indexes (range)
> are not valid any more for the new list.
> Does anybody has some suggestions on how to delete the elements
> properly?

clone the list, and then loop over the copy while deleting from the original

bye



More information about the Python-list mailing list