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)
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)
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)
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]