From: Javier Montoya on
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
From: superpollo on
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
From: Mark Lawrence on
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

From: Steven W. Orr on
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
From: Steven D'Aprano on
On Wed, 19 May 2010 03:53:44 -0700, 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?


Just delete the slice:

del myList[beginIndex:endIndex+1]

For small lists where you are deleting small chunks, this is the
simplest, most straight-forward way.


Alternatively, create a new list excluding the bits you would have
deleted, and assign it in place:

myList[:] = myList[:beginIndex] + myList[endIndex+1:]

Note carefully that you aren't just re-binding the name "myList", but
assigning to the slice myList[:].

Then there is the old-fashioned way: iterate over the list backwards,
deleting from the end towards the front.

for j in range(endIndex, beginIndex-1, -1):
del myList[j]


If your list is HUGE and you have very little memory, this is probably
the least worst way. It might be slow, but it will work.

Finally, we have this:

myList[beginIndex:] = myList[endIndex+1:]



--
Steven