python : list index out of range error while iteratively popping elements

list, python

Solution

You are reducing the length of your list `l` as you iterate over it, so as you approach the end of your indices in the range statement, some of those indices are no longer valid.

It looks like what you want to do is:

l = [x for x in l if x != 0]

which will return a copy of `l` without any of the elements that were zero (that operation is called a list comprehension, by the way). You could even shorten that last part to just `if x`, since non-zero numbers evaluate to `True`.

There is no such thing as a loop termination condition of `i < len(l)`, in the way you've written the code, because `len(l)` is precalculated before the loop, not re-evaluated on each iteration. You could write it in such a way, however:

i = 0
while i < len(l):
   if l[i] == 0:
       l.pop(i)
   else:
       i += 1

Problem

I have written a simple python program ``` l=[1,2,3,0,0,1] for i in range(0,len(l)): if l[i]==0: l.pop(i) ``` This gives me error 'list index out of range' on line `if l[i]==0:` After debugging I could figure out that `i` is getting incremented and list is getting reduced. However, I have loop termination condition `i < len(l)`. Then why I am getting such error?

Original source

Related problems