# Deletes items from a list, using another list as indices

I have a list `primeList` and another list `ls`. primeList is a list full of integers, and I need to delete the values from `ls` which have an index that is in `primeList`.

For example if `primelist = [1, 3 , 5]` and `ls = [1, 2, 3, 4, 5, 6, 7]`, then indexes 1, 3, and 5 should be removed from `ls`, making `ls = [1, 3, 5, 7]`

At the moment I'm trying to use this bit of code:

``````primeList = list(getPrimes())
ls = list(ls)
for i in primeList:
del ls[i]
return ls
```
```

This gives me the following error:

``````Traceback (most recent call last):
File "C:/Python34/My Scripts/NLP lab2/exec2.py", line 26, in <module>
otherList = delPrimes(myList)
File "C:/Python34/My Scripts/NLP lab2/exec2.py", line 18, in delPrimes
del ls[i]
IndexError: list assignment index out of range`
```
```

I believe this is because getPrimes is a larger list than ls, but I'm not sure how to work around this problem in Python?

EDIT - This is all of my current code:

``````def delPrimes(*ls):

def getPrimes():
L = []
for x in range(2, 230):
isPrime = True
for y in range(2, x):
if x % y == 0:
isPrime = False
if isPrime:
L.append(x)
return L

primeList = list(getPrimes())
ls = list(ls)
for i in primeList:
del ls[i]
return ls

myList = list(range(1, 51))

print(myList)
print("--" * 40)

otherList = delPrimes(myList)

print(otherList)
```
```

As part of some schoolwork we need to "Write a method in Python to delete the items at prime index locations in a list (up to index location 50). E.g. It will remove the item at index location 2, 3, 5, 7, … " I also believe we must use 'del' to do the deletion.

EDIT2:

``````for i in reversed(primeList):
if i <= len(ls):
del ls[i]
else:
continue
return ls
```
```

Use a list comprehension to avoid altering the list in place:

``````return [v for i, v in enumerate(ls) if i not in primeList]
```
```

You are deleting elements from the front of the list one by one, so the other elements each shift up one place. After the first deletion the rest of your indices are then off-by-one, then off-by-two, etc:

``````>>> ls = [1, 2, 3, 4, 5, 6, 7]
>>> del ls[1]
>>> ls
[1, 3, 4, 5, 6, 7]
>>> ls[3]
5
>>> del ls[3]
>>> ls
[1, 3, 4, 6, 7]
>>> ls[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
```
```

You can alter `ls` in place, but then you need to handle the indices in reverse so that you are only deleting indices that haven't yet shifted:

``````for index_to_remove in reversed(primeList):
del ls[index_to_remove]
```
```

but since you are already making a copy that's not necessary here.