delete items from list by indices

Chris Rebert clp2 at rebertia.com
Wed Sep 23 10:39:39 CEST 2009


On Wed, Sep 23, 2009 at 1:25 AM, blumenkraft <vohsrea at gmail.com> wrote:
> Hi,
>
> I have some list:
> x = [8, 9, 1, 7]
> and list of indices I want to delete from x:
> indices_to_delete = [0, 3], so after deletion x must be equal to [9,
> 1].
>
> What is the fastest way to do this? Is there any builtin?

#untested & unbenchmarked since it's 1am
offset = 0
for index in indices_to_delete:
    del x[index-offset]
    offset += 1

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list