-1

i need to remove all zeros from my array but it doesn't work!

array = [9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]
for i in array:
    if i == 0:
        array.remove(i)
print(array)

output:

[9, 9, 1, 2, 1, 1, 3, 1, 9, 0, 0, 0, 9]

i really don't know what the problem. Please helm me!

Andrey Kanava
  • 47
  • 1
  • 8

1 Answers1

1

There you go:

x = [9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]
x = [i for i in x if i != 0]
print(x)
Superman
  • 221
  • 2
  • 11