Removal of element from list while traversing causes the next element to be skipped
Neil Cerutti
mr.cerutti at gmail.com
Wed Jan 30 08:45:03 EST 2008
On 30 Jan 2008 05:20:49 -0800, Paul Rubin
<"http://phr.cx"@nospam.invalid> wrote:
> "Neil Cerutti" <mr.cerutti at gmail.com> writes:
> > Or one can put on his bellbottoms, horn-rimmed glasses, and wear a mullet:
> >
> > i = 0
> > while i < len(a):
> > if a[i] == 99:
> > del a[i]
> > else:
> > i += 1
>
> Quadratic time!! Yowch!! Back to the future:
>
> def rocket_science(xs):
> for x in xs:
> if x != 99:
> yield x
>
> a[:] = list(rocket_science(a))
Heh.
<lamely>It's probably a fairly peppy quadratic operation though.</lamely>
Besides, wherever will I find plutonium or a bolt of lightning?
--
Neil Cerutti <mr.cerutti+python at gmail.com>
More information about the Python-list
mailing list