how to delete multiple elements from a list

Carsten Geckeler geckeler at gmx.net
Tue Nov 21 19:10:58 EST 2000


On Tue, 21 Nov 2000, Rodrigo Senra wrote:

> 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()

This should be y.reverse()!
               ^
> x.del(y[0]) # not yet! must correct List Y too

Now it's ok for me.

Cheers, Carsten
-- 
Carsten Geckeler:  geckeler at gmx dot net





More information about the Python-list mailing list