remove elements incrementally from a list

J

Javier Montoya

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
 
M

Mark Lawrence

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
 
S

Steven W. Orr

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
 
S

Steven D'Aprano

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:]
 
J

Javier Montoya

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:]

Hi guys,

A big thanks to everybody, it's amazing!

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top