Extracting from a list
Terry Reedy
tejarex at yahoo.com
Thu Apr 11 12:04:02 EDT 2002
"Huaiyu Zhu" <huaiyu at gauss.almadan.ibm.com> wrote in message
news:slrnab9kqn.i8b.huaiyu at gauss.almadan.ibm.com...
> >This makes it possible to delete items from the list as the access
is
> >back to front.
>
> I've seen this several times in the past, in the context of making
the loop
> counter correct. But how does it fare in terms of performance?
Does the
> rest of the list get copied with each del? This sounds like an
O(nm)
> operation, where n is the list length and m is the number of del's.
Yes. To keep the operation O(n), scan the list and replace values to
be deleted with a delete flag (such as None) that does not otherwise
appear in the list. Then filter out the delete flags with (unchecked)
filter(lambda x: x != flag, flagged_list) .
Terry J. Reedy
More information about the Python-list
mailing list