1

I've noticed that in Python 3.6 when removing items from a list while looping through a list, as in the code below, the loop will be exited after the first time that an item is removed. Any ideas why this is? Does this always happen when you modify the iterible that you're looping over?

x = list(np.arange(5))

for i in x:
     if (i == 2) | (i == 3):
         x.remove(i)    

x
[0, 1, 3, 4]
Jed
  • 638
  • 1
  • 8
  • 17
  • yea, very similar question. Although, I'm not wondering how to do it, there are a variety of apparent methods, but rather why this happens. – Jed Mar 24 '18 at 16:25
  • Scroll down untill The Python 2 documentation 7.3. "The for statement" gives the same advice:" in the duplicate answer, it gives you an idea of the problem. – progmatico Mar 26 '18 at 21:11

0 Answers0