Dennis Lee Bieber
1/31/2008 8:53:00 AM
On Wed, 30 Jan 2008 23:49:46 -0800 (PST), "digisatori@gmail.com"
<digisatori@gmail.com> declaimed the following in comp.lang.python:
> li= [2,2,2,2,2,2,2,3,4,5,6,7,8,9]
> for x in li:
> if x == 2:
> print x
> li.remove(x)
Read the thread "Removal of element from list while traversing
causes the next element to be skipped"
When you remove an element, all the rest move "left" on position...
BUT the hidden loop index moves upwards to the next element based on the
original list length.
You delete the "first" 2, making the second "2" the first... But the
loop then accesses the NEW second "2" -- it doesn't see the new "first"
2.
list 2 2 2 2 3
index 0 1 2 3 4
delete first 2 (index 0)
list 2 2 2 3
index 0 1 2 3
delete second 2 (index 1)
list 2 2 3
index 0 1 2
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/