Deleting from a list
bvdpoel at uniserve.com
bvdpoel at uniserve.com
Tue Jan 1 21:14:08 EST 2002
Does python have a notion as to where the current item in a list is? I
have something like:
for l in lines:
if something ...
if something else:
delete l from list
Of course, this doesn't work. So, I tried:
for i in len(lines):
l=lines[i]
...
if ...:
lines.pop(i)
and this causes problems since 'i' can now point to out of range lines.
I ired:
for l in lines:
if...:
lines.remove(l)
But, this buggers the sequence. If you have [1,2,3,6,6,6,7] and delete
on the condition l==6, you only delete 2 '6's, not all 3.
So, finally, I did:
for i in len(lines)
l=lines(i)
...
if ..:
lines[i]=None
while(lines.count(None):
lines.remove(None)
My question: Is there a cleaner way to do all this? Having 2 loops seems
to be ugly.
--
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bvdpoel at uniserve.com
WWW: http://users.uniserve.com/~bvdpoel
More information about the Python-list
mailing list