Steven Hicken - 3 months ago 23

Python Question

So I was wondering how I can, using Python 2.7, most efficiently take a list of values used to represent indices like this: (but with a length of up to 250,000+)

`indices = [2, 4, 5]`

and remove that list of indices from a larger list like this: (3,000,000+ items)

`numbers = [2, 6, 12, 20, 24, 40, 42, 51]`

to get a result like this:

`[2, 6, 20, 42, 51]`

I'm looking for an efficient solution more than anything else. I know there are many ways to do this, however that's not my problem. Efficiency is. Also, this operation will have to be done many times and the lists will both get exponentially smaller. I do not have an equation to represent how much smaller they will get over time.

edit:

Numbers must remain sorted in a list the entire time or return to being sorted after the indices have been removed. The list called indices can either be sorted or not sorted. It doesn't even have to be in a list.

Answer

I have a suspicion that taking whole slices between the indices might be faster than the list comprehension

```
def remove_indices(numbers, indices):
result = []
i=0
for j in sorted(indices):
result += numbers[i:j]
i = j+1
result += numbers[i:]
return result
```