I have a list that I'm trying to iterate through while removing zeros(0) if any from the list.
The following is the code:
list = [1, 2, 0, 4, 5]
for i in range(len(list)):
if i == 0:
list.remove(i)
print(list[i])
Yes, the code works but raises the error IndexError: list index out of range
as bellow:
1 2 4 5 Traceback (most recent call last): File "M:/Python Workspace/PycharmProjects/untitled/scrapyard.py", line 6, in print(l[i]) IndexError: list index out of range
How do I do away with this error? Thanks.