remove elements incrementally from a list

Mark Lawrence breamoreboy at yahoo.co.uk
Wed May 19 07:38:16 EDT 2010


On 19/05/2010 11:53, Javier Montoya wrote:
> 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?
>
> Best wishes

You can delete the lot in one hit using a slice, see section 5.2 here
http://docs.python.org/tutorial/datastructures.html

Regards.

Mark Lawrence




More information about the Python-list mailing list