I'm not sure if I'm missing something, but I'm confused about this. I'm iterating over a list and removing some elements based on a condition. When the condition is met, it skips the next iteration. For example:
l = [1,1,2,3,1]
for i in l:
print(f'checking {i} in {l}')
if i == 1:
print(f'removing')
l.remove(i)
And this returns
checking 1 in [1, 1, 2, 3, 1]
removing
checking 2 in [1, 2, 3, 1]
checking 3 in [1, 2, 3, 1]
checking 1 in [1, 2, 3, 1]
removing
And then l=[2,3,1]
. I think what is happening is that it's iterating by index, so when something is removed, the current index in the iteration is off by one. Is there a way to do this without skipping elements?