iterating over collection, deleting entries

Discussion in 'Python' started by Patrick von Harsdorf, Apr 25, 2004.

  1. I want to iterate over a collection and delete all unwanted entries.

    for item in collection:
    del item

    of course doesn´t do anything useful. Two things come to mind:
    a) iterating backwards over the collection using indexing, something like:

    for i in range(len(collection)-1, -1, -1):
    item = collection
    if isBad(item) del collection

    b) duplicating the collection, iterating over one and deleting from the
    other.

    Both solutions seem both ugly and expensive to me, solution a)
    probably isn´t even O(x) anymore.

    Please tell me, what´s the best way to do this?


    --
    Patrick von Harsdorf
     
    Patrick von Harsdorf, Apr 25, 2004
    #1
    1. Advertising

  2. Re: iterating over collection, deleting entries => PLEASE IGNORE

    Sorry, I missed that SnuSnu just posed a very similar question.
    Please ignore my post...
     
    Patrick von Harsdorf, Apr 25, 2004
    #2
    1. Advertising

  3. On Sun, 25 Apr 2004 18:25:08 +0200, "Patrick von Harsdorf"
    <> wrote:

    >Please tell me, what´s the best way to do this?


    I'm pretty new to python, but normally this is done
    by a simple read-write approach. You iterate over
    the elements using a "read" pointer, and every time
    you find one you want to keep you just write it
    and increment the write pointer.
    In code...

    wrp = 0

    for x in container:
    if is_good(x):
    container[wrp] = x
    wrp += 1

    del container[wrp:]

    This is O(n) and doesn't require any additional memory

    I suppose however that it's not so common finding
    cases in which this is much better than...

    a = [x for x in a if is_good(x)]

    The latter will allocate a list for the result
    (instead of reusing the same), but can free the
    original (and this may be *better* than just
    resizing it - it may be better in C++, for example).

    HTH
    Andrea
     
    Andrea Griffini, Apr 25, 2004
    #3
  4. Patrick von Harsdorf

    Larry Bates Guest

    I would do:

    collection=[i for i in collection if not isBad(i)]

    Larry Bates
    Syscon, Inc.
    "Patrick von Harsdorf" <> wrote in message
    news:c6gop3$5p7$03$-online.com...
    > I want to iterate over a collection and delete all unwanted entries.
    >
    > for item in collection:
    > del item
    >
    > of course doesn´t do anything useful. Two things come to mind:
    > a) iterating backwards over the collection using indexing, something like:
    >
    > for i in range(len(collection)-1, -1, -1):
    > item = collection
    > if isBad(item) del collection
    >
    > b) duplicating the collection, iterating over one and deleting from the
    > other.
    >
    > Both solutions seem both ugly and expensive to me, solution a)
    > probably isn´t even O(x) anymore.
    >
    > Please tell me, what´s the best way to do this?
    >
    >
    > --
    > Patrick von Harsdorf
    >
    >
    >
    >
    >
     
    Larry Bates, Apr 26, 2004
    #4
    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. Rhino

    Adding/deleting Jar entries?

    Rhino, Feb 4, 2005, in forum: Java
    Replies:
    4
    Views:
    16,573
    Rhino
    Feb 4, 2005
  2. Øyvind Isaksen
    Replies:
    1
    Views:
    1,002
    Øyvind Isaksen
    May 18, 2007
  3. carl
    Replies:
    5
    Views:
    2,430
    James Kanze
    Nov 25, 2009
  4. Don Bruder
    Replies:
    3
    Views:
    1,005
    spikeysnack
    Aug 3, 2010
  5. Peter Bailey
    Replies:
    2
    Views:
    118
    Peter Bailey
    Sep 21, 2007
Loading...

Share This Page