How to del item of a list in loop?
Mitja
nun at example.com
Sat Jan 15 06:24:50 EST 2005
On Sat, 15 Jan 2005 15:27:08 -0500, skull <skullw at sina.com.cn> wrote:
> lst = [1, 2, 3]
> for i in lst:
> print i
> if i == 2:
> lst.remove(i)
>
> the result is:
>
> 1
> 2
As others have suggested, you can use a copy of the list.
Alternatively and depending on what you're trying to accomplish (how
complicated it is), lst = [i for i in lst if i!=2] might look better.
--
Mitja
More information about the Python-list
mailing list