I'm trying to delete an item inside a loop. I already learned that it is not possible. I'm seeing how to implement a comprehensive list.
But I still do not understand how it works. I tried to do this:
result = [x for x in range(0, len(list)) if list[x][0] != 0 and list[x][1] != 0]
But it does not return the list without the positions I want, it only returns the indices.
Code below so far and giving error:
list = [
[1,2],
[3,4],
[5,6],
[0,0],
[7,8]
]
for x in range(0, len(list)):
if list[x][0] == 0 and list[x][1] == 0:
del list[x]
Error:
IndexError: list index out of range
Now I did this:
result = [list[x] for x in range(0, len(list)) if list[x][0] != 0 and list[x][1] != 0]
... And returned the list without the positions I wanted. Is that so?