m1ghtfr3e
m1ghtfr3e

Reputation: 77

Why does it not remove all zeroes from the list

I haven't coded in Python for a long time and sometimes I'm quite confused. When I have an array, like eg.: arr = [0, 1, 0, 3, 12] and say:

for i in arr:
    if i == 0:
       arr.remove(i)

it is removing the two zeroes the way I want. But if the array is something like: arr = [0, 0, 1], with the above method, one 0 will remain. So could someone explain me why the behavior is like that? I don't find an explanation for this.

Upvotes: 2

Views: 222

Answers (3)

JrmDel
JrmDel

Reputation: 462

I think I found why your method doesn't work. The problem comes from the way you iterate.

In your example, your function seems to work for arr = [0,1,0,3,12] but not on your second array arr2 = [0,0,2] and returns [0,2]. One interesting thing to investigate then, is the fact that in your second example, you have two consecutive zeros.

Take a look at this code and try to execute it :

for i in arr:
        print('i = '+str(i))
        if(i == 0):
            arr.remove(i)

With your first array, you noticed that your output is the one you expected but that was lucky. As a matter of fact, if you run the code above, you would see that it prints in your console :

> i = 0
> i = 0
> i = 12

So, actually, this means that your remove statement changes the array you iterate on. After a deletion, you skip an element in your array.

This means you should prefer another way, like the ones suggested in comments.

Hope this helps

Upvotes: 1

kederrac
kederrac

Reputation: 17322

you can filter out your zeros with the built-in function filter:

arr = list(filter(None, arr))

you have to pay attention if you use filter function with None as first parameter, this will apply bool over your items if you have elements like None, 0 or the empty string '' the result will be the same, False and all these elements will be filtered out, for safety reasons you may use:

arr = list(filter(lambda x: x != 0 , arr))

Upvotes: 0

Óscar López
Óscar López

Reputation: 236034

Better try this:

arr = [n for n in arr if n != 0]

This uses a list comprehension, it's a lot safer than what you're doing: removing the elements at the same time you're iterating is a bad idea, and you'll experience problems such as elements not being removed.

This is because the list size is reduced and the iterator traversing it will find less elements than it was expecting when the iteration began.

Upvotes: 4

Related Questions