Deleting from a list
Jason Orendorff
jason at jorendorff.com
Tue Jan 1 22:42:19 EST 2002
> My question: Is there a cleaner way to do all this? Having 2 loops seems
> to be ugly.
i = 0
while i < len(lines):
line = lines[i]
...
if should_delete_this(line):
del lines[i]
continue # This intentionally doesn't increment i.
...
i += 1
But I usually just build the result list as I go instead. It's clearer.
ok_lines = []
for line in lines:
...
...
if ok(line):
ok_lines.append(line)
## Jason Orendorff http://www.jorendorff.com/
More information about the Python-list
mailing list