-1

Why does not every 0 get removed from the list?

list = [0,0,5,3,0,8,0,4] 

for num in list:
    if num == 0:
        list.remove(0)

print(list)
ruohola
  • 21,987
  • 6
  • 62
  • 97
Emil berg
  • 15
  • 3
  • 4
    Actually, it's because you're changing the size of `list`, while iterating over it. This is never a good idea. It's also not a good idea to shadow the `list` keyword. – Paul M. Nov 30 '19 at 22:21
  • 3
    Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) So either use a list comprehension or use a copy of your list. – Cleb Nov 30 '19 at 22:22
  • 1
    The title of the question is irrelevant. You have nothing related to indexes in the question – Tomerikoo Nov 30 '19 at 22:37

1 Answers1

4

Because you are mutating the list while looping over it, and that is almost always a bad idea. Here's a demonstration of what happens:

list = [0,0,5,3,0,8,0,4] 

for num in list:
    print(num, list)
    if num == 0:
        list.remove(0)

print(list)

Output

0 [0, 0, 5, 3, 0, 8, 0, 4]
5 [0, 5, 3, 0, 8, 0, 4]
3 [0, 5, 3, 0, 8, 0, 4]
0 [0, 5, 3, 0, 8, 0, 4]
0 [5, 3, 0, 8, 0, 4]
[5, 3, 8, 0, 4]

And here's a relevant section from the documenation, which tells that this is bad idea:

Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g.,

for x in a[:]:
    if x < 0: a.remove(x)

Source

You can use a very simple list comprehension to make this work:

list_ = [0,0,5,3,0,8,0,4]  # list shadows the built-in name, don't use it
list_ = [num for num in list_ if num != 0]
print(list_)

Output:

[5, 3, 8, 4]
ruohola
  • 21,987
  • 6
  • 62
  • 97