remove elements incrementally from a list

Steven W. Orr steveo at syslang.net
Wed May 19 09:47:22 EDT 2010


On 5/19/2010 6:53 AM, Javier Montoya wrote:

> 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?

How about using numpy?

to_be_deleted is an array of indexes to be deleted from myList. You seem to 
already have that list created but your list is contiguous. Just in case it 
isn't, you can say stuff like:

to_be_deleted = numpy.where( myList > allowed_or_something )
myList = numpy.delete( myList, to_be_deleted )


-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



More information about the Python-list mailing list