list.pop and print doing funny things

Gordon Williams g_will at cyberus.ca
Tue Sep 21 20:39:34 CEST 2004


An easy question....

Why do I get the x printed twice in the sample below? It is like print x is
being executed twice in each loop.


>>> l = [1,2,3,4]
>>> for x in l[:]:
...  print x
...  l.pop(l.index(x))
...  print l
...
1
1
[2, 3, 4]
2
2
[3, 4]
3
3
[4]
4
4
[]
>>>

Regards,

Gordon Williams




More information about the Python-list mailing list