deleting items within a for loop - mutable indices
Roger Binns
rogerb at rogerbinns.com
Sat Apr 24 05:53:30 EDT 2004
SnuSnu wrote:
> are deleted first), but this is not very elegant or quick:
Here is a list comprehension that makes a new list
without the deletion_list items. It does have the virtue
of being a one liner :-)
newx=[x[i] for i in range(len(x)) if i not in deletion_list]
You will need to do timings to see what method is actually
the fastest. Don't forget to do so with list sizes the
same as you expect to be using in your real program. Search
archives for this group for 'timeit' to see examples of
how to do timings.
(I think that the size of the deletion_list as a proportion
of the original list is what best determines the relative
speeds of the various methods available).
Roger
More information about the Python-list
mailing list