i found this code and i am just confused why are the even numbers retained in list x when the remove method is at the top of the for loop, i tried to print the list for each iteration and it shows the first iteration is 1, gets removed from x then appended to y, but the next iteration of i is now 3 not 2
x = [1,2,3,4,5,6,7,8,9,10]
y = []
for i in x:
x.remove(i)
if i%2 == 0:
continue
elif len(x) == 5:
break
y.append(i)
print(x) # output [2, 4, 6, 8, 10]
print(y) # output [1, 3, 5, 7]
adding print before the remove method
...
print(i)
print(x)
x.remove(i)
...
as you can see the next iteration is 3 not 2 when 2 is still in list x
1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3
[2, 3, 4, 5, 6, 7, 8, 9, 10]
5
[2, 4, 5, 6, 7, 8, 9, 10]
7
[2, 4, 6, 7, 8, 9, 10]
9
[2, 4, 6, 8, 9, 10]