Example:
l = [1,2,3,4,5,6,7,8,9,0]
for i in l:
print i,l
l.remove(i)
Returns:
1 [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
3 [2, 3, 4, 5, 6, 7, 8, 9, 0]
5 [2, 4, 5, 6, 7, 8, 9, 0]
7 [2, 4, 6, 7, 8, 9, 0]
9 [2, 4, 6, 8, 9, 0]
So why is there only 5 spins? I expect it to turn 10 times.
Can someone explain it to me step by step?
Notice how the printed number jumps over one number in the list? That is because, i points to a position in the list and once you delete
ilist “shifts” one position to the left and than wheniis incremented, it in fact moves two positions to the right. So after the fifth iteration you would be in the position to the right of0or out of the list.