I have a list L.
I can delete element i by doing:
del L[i]
But what if I have a set of non contiguous indexes to delete?
I=set([i1, i2, i3,...])
Doing:
for i in I:
del L[i]
Won't work.
Any ideas?
I have a list L.
I can delete element i by doing:
del L[i]
But what if I have a set of non contiguous indexes to delete?
I=set([i1, i2, i3,...])
Doing:
for i in I:
del L[i]
Won't work.
Any ideas?
for i in I:
del L[i]
won't work, because (depending on the order) you may invalidate the iterator -- this will usually show up as some items which you intended to delete remaining in the list.
It's always safe to delete items from the list in the reverse order of their indices. The easiest way to do this is with sorted():
for i in sorted(I, reverse=True):
del L[i]
Eine Minuten bitte, Ich hap eine kleine Problemo avec diese Religione. -- Eddie Izzard (doing his impression of Martin Luther)
Deleting by reverse-iterating over a list to preserve the iterator is a common solution to this problem. But another solution is to change this into a different problem. Instead of deleting items from the list using some criteria (in your case, the index exists in a list of indexes to be deleted), create a new list that leaves out the offending items.
L[:] = [ item for i,item in enumerate(L) if i not in I ]
For that matter, where did you come up with the indexes in I
in the first place? You could combine the logic of getting the indexes to be removed and building the new list. Assuming this is a list of objects and you only want to keep those that pass an isValid
test:
L[:] = [ item for item in L if item.isValid() ]
This is much more straightforward than:
I = set()
for i in range(len(L)):
if not L[i].isValid():
I.add(i)
for i in sorted(I, reverse=True):
del L[i]
For the most part, I turn any question about "how to delete from a list the items that I don't want" into "how to create a new list containing just the items I want".
EDITED: changed "L = ..." to "L[:] = ..." per Alex Martelli's answer to this question.
If your original list data can safely be turned into a set (i.e. all unique values and doesn't need to maintain order), you could also use set operations:
Lset = set(L)
newset = Lset.difference(I)
You could also maybe do something with a Bag/Multiset, though it probably isn't worth the effort. Paul McGuire's second listcomp solution is certainly best for most cases.