I'm doing an exercise where a function takes a list and an integer n
and deletes every element if its index i
satisfies (i+1)%n==0
. Here's what I've come up with:
def f(lst, n):
for i in range(len(lst)):
if (i+1)%n==0:
del lst[i]
However, it gives me this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pop.py", line 4, in f
del lst[i]
IndexError: list assignment index out of range
The index pointer i
doesn't seem to be able to run out of range? What causes this to happen?