how to delete multiple elements from a list
Rodrigo Senra
rodsenra at correionet.com.br
Tue Nov 21 17:56:00 EST 2000
Mikael Olofsson wrote:
>
> On 14-Nov-00 etsang at my-deja.com wrote:
> > 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.
>
> You _can_ do that with a loop. Just make sure that you have Y in reverse
> order.
>
> Y.sort()
> Y.reverse()
> for y in Y:
> del X[y]
x=[1,2,3,4]
y=[1] # want to delete element-2 at index 1
x.reverse()
x.del(y[0]) # not yet! must correct List Y too
Mikael Olofsson wrote:
> I will leave that one to someone else.
# that's my call!
ny=map(lambda a,b=len(x)-1:b-a,y) # to fix y list saving at ny
# That should correct the indexes 4 u. But check it out, I haven't
tested it
HTH
--
Rodrigo Senra
Computer Engineer (GPr Sistemas Ltda) rodsenra at correionet.com.br
MSc Student of Reflection (IC- UNICAMP) Rodrigo.Senra at ic.unicamp.br
http://www.ic.unicamp.br/~921234 (see also http://www.gpr.com.br)
More information about the Python-list
mailing list