tags:

views:

726

answers:

3

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?

+12  A: 

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
Mark Rushakoff
+6  A: 

The expression len(l) is evaluated only one time, at the moment the range() builtin is evaluated. The range object constructed at that time does not change; it can't possibly know anything about the object l.

P.S. l is a lousy name for a value! It looks like the numeral 1, or the capital letter I.

Jonathan Feinberg
A: 

You're changing the size of the list while iterating over it, which is probably not what you want and is the cause of your error.

Edit: As others have answered and commented, list comprehensions are better as a first choice and especially so in response to this question. I offered this as an alternative for that reason, and while not the best answer, it still solves the problem.

So on that note, you could also use filter, which allows you to call a function to evaluate the items in the list you don't want.

Example:

>>> l = [1,2,3,0,0,1]
>>> filter(lambda x: x > 0, l)
[1, 2, 3]

Live and learn. Simple is better, except when you need things to be complex.

jathanism
You don't even need the lambda, since 0 evaluates to False. `filter(None, l)`
Steve Losh
In fact his lambda condition is wrong. But +1 for `filter`.
int3
@Steve Losh - This is what I love about SO... Learning simple little tricks like that to save me keystrokes in the longrun! Thanks!
jathanism
-1: Why do you point a beginner to this legacy way? List comprehensions are now the preferred way to do this.
nikow
-1 for filter or map. You should always use a list comprehension if it will do the job.
Nick Bastin
You guys are right, but `filter` is still an important part of the toolkit for when you need more advanced evaluation.
jathanism