How to delete an element from a list while iterating it in Python?

Posted by bodacydo on Stack Overflow See other posts from Stack Overflow or by bodacydo
Published on 2010-03-14T15:36:38Z Indexed on 2010/03/14 15:45 UTC
Read the original article Hit count: 406

Filed under:

Suppose I have a list of numbers:

L = [1, 2, 3, 4, 5]

How do I delete an element, let's say 3, from the list while I iterate it?

I tried the following code but it didn't do it:

for el in L:
  if el == 3:
    del el

Any ideas?

Thanks, Boda Cydo.

© Stack Overflow or respective owner

Related posts about python