[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

list traversal and remove

digisatori@gmail.com

1/31/2008 7:50:00 AM

I supposed the below code will print seven 2 and generate the list li
without 2.
Strangely it only print four 2. If you change the number of 2 in the
list, the results are all beyond expectation.
I know the other way to achieve the expected goal, but why this is
happening? Could somebody enlight me?

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)
4 Answers

Dennis Lee Bieber

1/31/2008 8:53:00 AM

0

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/

Arnaud Delobelle

1/31/2008 8:56:00 AM

0

On Jan 31, 7:49 am, "digisat...@gmail.com" <digisat...@gmail.com>
wrote:
> I supposed the below code will print seven 2 and generate the list li
> without 2.
> Strangely it only print  four 2. If you change the number of 2 in the
> list, the results are all beyond expectation.
> I know the other way to achieve the expected goal, but why this is
> happening? Could somebody enlight me?
>
> 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)

There's just been a thread on this:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/290623b...

--
Arnaud

Steven D'Aprano

1/31/2008 9:55:00 AM

0

On Wed, 30 Jan 2008 23:49:46 -0800, digisatori@gmail.com wrote:

> I supposed the below code will print seven 2 and generate the list li
> without 2.
> Strangely it only print four 2. If you change the number of 2 in the
> list, the results are all beyond expectation. I know the other way to
> achieve the expected goal, but why this is happening? Could somebody
> enlight me?

Do not modify a list at the same time that you are traversing it.

If you look at the archives (say, on Google Groups, or any number of
other places), this topic was just discussed yesterday and earlier today.

See the thread with subject line:

"Removal of element from list while traversing causes the next element
to be skipped"

As for why it is happening... you have code that looks like this:

for x in li:
if x == 2:
print x
li.remove(x)


Consider that "under the hood", the for-loop looks something like this:

i = 0
while i < the length of the list:
set x equal to the item in the i-th position
execute the loop block of code
i += 1

If you start deleting or inserting items in the middle of the loop, the
index won't be pointing where you expect.



--
Steven

digisatori@gmail.com

1/31/2008 1:47:00 PM

0

Thank you very much for the great anwsers. You guys save my sleep
today.