Removing Item From List - during iteration - what's wrong with this idiom ?

Posted by monojohnny on Stack Overflow See other posts from Stack Overflow or by monojohnny
Published on 2010-05-24T11:54:40Z Indexed on 2010/05/24 12:01 UTC
Read the original article Hit count: 176

Filed under:
|
|
|

As an experiment, I did this:

letters=['a','b','c','d','e','f','g','h','i','j','k','l']
for i in letters:
    letters.remove(i)
print letters

The last print shows that not all items were removed ? (every other was).

IDLE 2.6.2      
>>> ================================ RESTART ================================
>>> 
['b', 'd', 'f', 'h', 'j', 'l']
>>> 

What's the explanation for this ? How it could this be re-written to remove every item ?

© Stack Overflow or respective owner

Related posts about python

Related posts about list