how to delete multiple elements from a list

etsang at my-deja.com etsang at my-deja.com
Wed Nov 15 23:25:57 CET 2000


Hi I have another question: What if List Y is something like:
(1,2,3,6,7,8) and List X is (0,1,2,3,4,5,6,7)
When List Y is in deceding order:
(8,7,6,1,2,3)

for each iteration of for loop, List X will have one element deleted.
But after element 3 is deleted, List X no longer has element in
postions 8,7, or 6.
hoe can I write the code so that I do not have any restriction on what
List Y has????

Thanks
In article <uaoQ5.24146$U46.888647 at news1.sttls1.wa.home.com>,
  "Greg Jorgensen" <gregj at pobox.com> wrote:
>
> <etsang at my-deja.com> wrote in message news:8usjl6
$vea$1 at nnrp1.deja.com...
> > 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
>
> y.sort()
> y.reverse()
> for p in y:
>     try:
>         del x[p]
>     except IndexError:
>         pass
>
> --
> Greg Jorgensen
> Deschooling Society
> Portland, Oregon, USA
> gregj at pobox.com
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list