delete items from list by indices

Daniel Stutzbach daniel at stutzbachenterprises.com
Wed Sep 23 21:18:34 EDT 2009


On Wed, Sep 23, 2009 at 3:48 AM, Peter Otten <__peter__ at web.de> wrote:

> Why's that obsession with speed?
>

Well, most of the solutions posted so far are O(n**2), which may be
noticeably slow if the list is of considerable length.  I wonder if the
original poster ran in to that problem.

>>> items = ["a", "b", "c", "d"]
> >>> delenda = set([0, 3])
> >>> items = [item for index, item in enumerate(items) if index not in
> delenda]
> >>> items
> ['b', 'c']


... except for that one.  That's O(n).  Very nice. :-)

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090923/edf926a4/attachment-0001.html>


More information about the Python-list mailing list