How to del item of a list in loop?
John Machin
sjmachin at lexicon.net
Sun Jan 16 15:38:26 EST 2005
skull wrote:
> According to Nick's article, I added three 'reversed' methods to your
provided
> test prog. and the result turned out method_reversed is faster than
others except the 'three' case.
> Following is my modified version:
[snip]
> def method_reversed_idx(lst):
> idx = 0
> for i in reversed(lst):
> if i == 2:
> del lst[idx]
> idx += 1
There appears to be a problem with this one:
>>> def method_reversed_idx(lst):
... idx = 0
... for i in reversed(lst):
... if i == 2:
... del lst[idx]
... idx += 1
...
>>> lst=[1,2,3];method_reversed_idx(lst);print lst
[1, 3]
>>> lst=[2,1,3];method_reversed_idx(lst);print lst
[2, 1]
>>> lst=[1,3,2];method_reversed_idx(lst);print lst
[3]
>>>
More information about the Python-list
mailing list