Deleting from a list
Jason Orendorff
jason at jorendorff.com
Tue Jan 1 22:42:19 EST 2002
More information about the Python-list mailing list
Tue Jan 1 22:42:19 EST 2002
- Previous message (by thread): Have a look at Albatross (was Re: Server side scripting with PSP)
- Next message (by thread): Deleting from a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> 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/
- Previous message (by thread): Have a look at Albatross (was Re: Server side scripting with PSP)
- Next message (by thread): Deleting from a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list