vec = [-4,-2,0,2,4]
for x in vec:
if x<0:
print(x)
vec.remove(x)
print(vec)
This is a program to remove negative numbers and retain positive numbers in a list Without the .remove(x), i printed out -4,-2 But with .remove(x),Why when I print(x), it prints out only -4, and only -4 is removed? Isn't supposedly -2 will also be printed and removed? Isn't supposedly the vec list finally retains 0,2,4 ?
I have tried putting if x <0 or x==-2 , it doesn't work either instead returning me the same problems
I know I can build a new list like vectwo = [x for x in vec if x>0 or x=0] but I just can't figured out the logical fallacy in the iteration I built to remove negative numbers.