how to delete multiple elements from a list
Robert Roy
rjroy at takingcontrol.com
Sun Nov 19 10:35:26 EST 2000
On Tue, 14 Nov 2000 23:59:39 GMT, etsang at my-deja.com wrote:
>Hi,
>
>I have a List X with N number of items in it. I have another List Y
>which contains the postion of the elements to be deleted from List X.
>I cannot do a for loop to delete that because for each iteration, List
>X will be changed and there will be sliperage.
>
>How can I delete all the items as specified by the positions in Y at
>one time? Also both List X and List Y are known in run time only.
>Thanks
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.
In one of your posts you said that the positions in Y are unique. Are
the items in X unique? If so you might do something like this (at the
cost of a list copy):
X2 = X[:] # make a copy of X
for index in Y:
X.remove[X2[index]]
X2=[] # deallocate X2
If not then :
Y.sort()
Y.reverse()
for index in Y:
del X[index]
is probably the best solution.
Bob
More information about the Python-list
mailing list