delete items from list by indices

B

blumenkraft

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?

Thanks.
 
C

Chris Rebert

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
 
P

Peter Otten

blumenkraft said:
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?

Why's that obsession with speed?
items = ["a", "b", "c", "d"]
delenda = [0, 3]
for i in sorted(delenda, reverse=True):
.... del items
....['b', 'c']

items = ["a", "b", "c", "d"]
delenda = set([0, 3])
items = [item for index, item in enumerate(items) if index not in delenda]
items
['b', 'c']

If you really need to modify the list in place change

items = [item for ...]

to

items[:] = [item for ...]

Try these and come back to complain if any of the above slows down your
script significantly...

Peter
 
I

Ishwor

Hi

2009/9/23 blumenkraft said:
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?

Try this-
x = [8, 9, 1, 7]
[x.pop(i) for i in sorted(indices_to_delete,reverse=True)] [7, 8]
x
[9, 1]

Built-in used here is `sorted' and method on list used here is `pop'.
With regards to efficiency you may want to use the methods of list
which is more intuitive afaik and useful as its more reflective of
effect on the type list. It's a trivial choice here but later it might
help.
 
B

blumenkraft

blumenkraft said:
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?

Why's that obsession with speed?
items = ["a", "b", "c", "d"]
delenda = [0, 3]
for i in sorted(delenda, reverse=True):

... š š del items
...>>> items

['b', 'c']
items = ["a", "b", "c", "d"]
delenda = set([0, 3])
items = [item for index, item in enumerate(items) if index not in delenda]
items

['b', 'c']

If you really need to modify the list in place change

items = [item for ...]

to

items[:] = [item for ...]

Try these and come back to complain if any of the above slows down your
script significantly...

Peter


Thanks, it helped (I didn't know about keyword argument "reverse" in
sorted function)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top