delete items from list by indices

Discussion in 'Python' started by blumenkraft, Sep 23, 2009.

  1. blumenkraft

    blumenkraft Guest

    Hi,

    I have some list:
    x = [8, 9, 1, 7]
    and list of indices I want to delete from x:
    indices_to_delete = [0, 3], so after deletion x must be equal to [9,
    1].

    What is the fastest way to do this? Is there any builtin?

    Thanks.
     
    blumenkraft, Sep 23, 2009
    #1
    1. Advertising

  2. blumenkraft

    Chris Rebert Guest

    On Wed, Sep 23, 2009 at 1:25 AM, blumenkraft <> wrote:
    > Hi,
    >
    > I have some list:
    > x = [8, 9, 1, 7]
    > and list of indices I want to delete from x:
    > indices_to_delete = [0, 3], so after deletion x must be equal to [9,
    > 1].
    >
    > What is the fastest way to do this? Is there any builtin?


    #untested & unbenchmarked since it's 1am
    offset = 0
    for index in indices_to_delete:
    del x[index-offset]
    offset += 1

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Sep 23, 2009
    #2
    1. Advertising

  3. blumenkraft

    Peter Otten Guest

    blumenkraft wrote:

    > I have some list:
    > x = [8, 9, 1, 7]
    > and list of indices I want to delete from x:
    > indices_to_delete = [0, 3], so after deletion x must be equal to [9,
    > 1].
    >
    > What is the fastest way to do this? Is there any builtin?


    Why's that obsession with speed?

    >>> items = ["a", "b", "c", "d"]
    >>> delenda = [0, 3]
    >>> for i in sorted(delenda, reverse=True):

    .... del items
    ....
    >>> items

    ['b', 'c']


    >>> 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']

    If you really need to modify the list in place change

    items = [item for ...]

    to

    items[:] = [item for ...]

    Try these and come back to complain if any of the above slows down your
    script significantly...

    Peter
     
    Peter Otten, Sep 23, 2009
    #3
  4. blumenkraft

    Ishwor Guest

    Hi

    2009/9/23 blumenkraft <>:
    > Hi,
    >
    > I have some list:
    > x = [8, 9, 1, 7]
    > and list of indices I want to delete from x:
    > indices_to_delete = [0, 3], so after deletion x must be equal to [9,
    > 1].
    >
    > What is the fastest way to do this? Is there any builtin?


    Try this-
    >>> x = [8, 9, 1, 7]
    >>> [x.pop(i) for i in sorted(indices_to_delete,reverse=True)]

    [7, 8]
    >>> x

    [9, 1]

    Built-in used here is `sorted' and method on list used here is `pop'.
    With regards to efficiency you may want to use the methods of list
    which is more intuitive afaik and useful as its more reflective of
    effect on the type list. It's a trivial choice here but later it might
    help.
    --
    Regards,
    Ishwor Gurung
     
    Ishwor, Sep 23, 2009
    #4
  5. blumenkraft

    blumenkraft Guest

    On 23 ÓÅÎ, 12:48, Peter Otten <> wrote:
    > blumenkraft wrote:
    > > I have some list:
    > > x = [8, 9, 1, 7]
    > > and list of indices I want to delete from x:
    > > indices_to_delete = [0, 3], so after deletion x must be equal to [9,
    > > 1].

    >
    > > What is the fastest way to do this? Is there any builtin?

    >
    > Why's that obsession with speed?
    >
    > >>> items = ["a", "b", "c", "d"]
    > >>> delenda = [0, 3]
    > >>> for i in sorted(delenda, reverse=True):

    >
    > ... š š del items
    > ...>>> items
    >
    > ['b', 'c']
    >
    > >>> 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']
    >
    > If you really need to modify the list in place change
    >
    > items = [item for ...]
    >
    > to
    >
    > items[:] = [item for ...]
    >
    > Try these and come back to complain if any of the above slows down your
    > script significantly...
    >
    > Peter


    Thanks, it helped (I didn't know about keyword argument "reverse" in
    sorted function)
     
    blumenkraft, Sep 23, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SnuSnu
    Replies:
    6
    Views:
    336
    Steven Rumbalski
    Apr 25, 2004
  2. Replies:
    5
    Views:
    336
    Bengt Richter
    Dec 10, 2004
  3. Girish Sahani
    Replies:
    7
    Views:
    5,491
    Larry Bates
    Jun 13, 2006
  4. Replies:
    3
    Views:
    389
    Gerard Flanagan
    Jun 30, 2006
  5. ÃCø¯
    Replies:
    4
    Views:
    689
    ÃCø¯
    Jan 22, 2008
Loading...

Share This Page