Removal of element from list while traversing causes the nextelement to be skipped

N

Neil Cerutti

Neil Cerutti said:
Or one can put on his bellbottoms, horn-rimmed glasses, and wear a mullet:

i = 0
while i < len(a):
if a == 99:
del a
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?
 
H

Hrvoje Niksic

Paul Rubin said:
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))

I call "useless use of list"!

a[:] = rocket_science(a)

:)
 
C

cokofreedom

Anyone else noticed that the OP has not actually replied to any of the
suggestions...
 
W

William McBrine

Anyone else noticed that the OP has not actually replied to any of the
suggestions...

Sorry. I was just fascinated at the turns it was taking. But the first
answer was fine for me:

for name in apps[:]:
etc.

Thanks all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top