Delete all items in the list

Discussion in 'Python' started by Clarendon, Feb 26, 2009.

  1. Clarendon

    Clarendon Guest

    Hi. This must be a simple command but I just can't find it in the
    Phthon manual. How do I delete all items with a certain condition from
    a list? For instance:

    L=['a', 'b', 'c', 'a']

    I want to delete all 'a's from the list.
    But if L.remove('a') only deletes the first 'a'.

    How do you delete all 'a's?
    I would really appreciate your help.

    Thanks.
     
    Clarendon, Feb 26, 2009
    #1
    1. Advertising

  2. Clarendon

    Chris Rebert Guest

    On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <> wrote:
    > Hi. This must be a simple command but I just can't find it in the
    > Phthon manual. How do I delete all items with a certain condition from
    > a list? For instance:
    >
    > L=['a', 'b', 'c', 'a']
    >
    > I want to delete all 'a's from the list.
    > But if L.remove('a') only deletes the first 'a'.
    >
    > How do you delete all 'a's?


    There are several ways. I'd go with a list comprehension:

    L = [i for i in L if i != 'a']

    Or to modify the list in-place:

    L[:] = [i for i in L if i != 'a']

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Feb 26, 2009
    #2
    1. Advertising

  3. Clarendon a écrit :
    > Hi. This must be a simple command but I just can't find it in the
    > Phthon manual. How do I delete all items with a certain condition from
    > a list? For instance:
    >
    > L=['a', 'b', 'c', 'a']
    >
    > I want to delete all 'a's from the list.
    > But if L.remove('a') only deletes the first 'a'.
    >
    > How do you delete all 'a's?


    L[:] = [item for item in L if item != 'a']
     
    Bruno Desthuilliers, Feb 26, 2009
    #3
  4. Clarendon

    Boris Borcic Guest

    Chris Rebert wrote:
    > On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <> wrote:

    ....
    >> L=['a', 'b', 'c', 'a']
    >>
    >> I want to delete all 'a's from the list.
    >> But if L.remove('a') only deletes the first 'a'.
    >>
    >> How do you delete all 'a's?

    >
    > There are several ways. I'd go with a list comprehension:


    and for a couple other ways

    while 'a' in L : L.remove('a')

    L = filter('a'.__ne__,L)
     
    Boris Borcic, Feb 26, 2009
    #4
  5. En Thu, 26 Feb 2009 11:00:30 -0200, Boris Borcic <>
    escribió:

    > Chris Rebert wrote:
    >> On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <> wrote:

    > ...
    >>> L=['a', 'b', 'c', 'a']
    >>>
    >>> I want to delete all 'a's from the list.
    >>> But if L.remove('a') only deletes the first 'a'.
    >>>
    >>> How do you delete all 'a's?

    >> There are several ways. I'd go with a list comprehension:

    >
    > and for a couple other ways
    >
    > while 'a' in L : L.remove('a')


    This is probably the worst way, takes cuadratic time (and two searches per
    loop).

    > L = filter('a'.__ne__,L)


    And this is probably the fastest. But not all types define __ne__ so a
    more generic answer would be:

    from functools import partial
    from operator import ne
    L = filter(partial(ne, 'a'), L)

    Of course, this is only relevant if L is large and there are many
    duplicates. In other cases I'd stick with the list comprehension as posted
    by Chris Rebert.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Feb 26, 2009
    #5
  6. Clarendon

    John Posner Guest

    >>
    >> > L = filter('a'.__ne__,L)

    >>
    >> And this is probably the fastest. But not all types define
    >> __ne__ so a
    >> more generic answer would be:
    >>
    >> from functools import partial
    >> from operator import ne
    >> L = filter(partial(ne, 'a'), L)
    >>


    And don't forget this "traditional" solution:

    >>> L=['a', 'b', 'c', 'a']
    >>> filter(lambda arg: arg != 'a', L)

    ['b', 'c']

    -John





    E-mail message checked by Spyware Doctor (6.0.0.386)
    Database version: 5.11850
    http://www.pctools.com/en/spyware-doctor-antivirus/
     
    John Posner, Feb 26, 2009
    #6
  7. Clarendon

    John Posner Guest

    functools.partial (was: Delete all items in the list)

    >>
    >> from functools import partial
    >> from operator import ne
    >> L = filter(partial(ne, 'a'), L)
    >>


    I learned about functools.partial only recently (on this list). functools is
    implemented in C, not Python, so I couldn't answer this question for myself:

    Is functools.partial completely equivalent to a manually-coded closure?

    Help, please.

    -John





    E-mail message checked by Spyware Doctor (6.0.0.386)
    Database version: 5.11850
    http://www.pctools.com/en/spyware-doctor-antivirus/
     
    John Posner, Feb 26, 2009
    #7
  8. Clarendon

    Terry Reedy Guest

    Gabriel Genellina wrote:

    >
    >> L = filter('a'.__ne__,L)

    >
    > And this is probably the fastest. But not all types define __ne__


    In Py3, all classes inherit .__ne__ from 'object'.
     
    Terry Reedy, Feb 26, 2009
    #8
  9. Clarendon

    Steve Holden Guest

    Terry Reedy wrote:
    > Gabriel Genellina wrote:
    >
    >>
    >>> L = filter('a'.__ne__,L)

    >>
    >> And this is probably the fastest. But not all types define __ne__

    >
    > In Py3, all classes inherit .__ne__ from 'object'.
    >

    Isn't that inherited method just an identity comparison?

    However in this particular case the main point is that str *does*
    implement __ne__. So as long as that built-in method doesn't call the
    other operand's __ne__ there should be no problem.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Feb 26, 2009
    #9
  10. Clarendon

    Terry Reedy Guest

    Steve Holden wrote:
    > Terry Reedy wrote:
    >> Gabriel Genellina wrote:
    >>
    >>>> L = filter('a'.__ne__,L)
    >>> And this is probably the fastest. But not all types define __ne__

    >> In Py3, all classes inherit .__ne__ from 'object'.
    >>

    > Isn't that inherited method just an identity comparison?


    Yes. And so is, by default, the snipped slower proposed alternative of
    lambda x: a!= x.

    > However in this particular case the main point is that str *does*
    > implement __ne__. So as long as that built-in method doesn't call the
    > other operand's __ne__ there should be no problem.
    >
    > regards
    > Steve
     
    Terry Reedy, Feb 26, 2009
    #10
  11. Clarendon

    Steve Holden Guest

    Terry Reedy wrote:
    > Steve Holden wrote:
    >> Terry Reedy wrote:
    >>> Gabriel Genellina wrote:
    >>>
    >>>>> L = filter('a'.__ne__,L)
    >>>> And this is probably the fastest. But not all types define __ne__
    >>> In Py3, all classes inherit .__ne__ from 'object'.
    >>>

    >> Isn't that inherited method just an identity comparison?

    >
    > Yes. And so is, by default, the snipped slower proposed alternative of
    > lambda x: a!= x.
    >

    But shouldn't that have been lambda x: 'a' != x, which would have
    invoked the string (3.0, Unicode) comparison method?

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Feb 26, 2009
    #11
  12. En Thu, 26 Feb 2009 22:08:26 -0200, Peter Billam <>
    escribió:

    > On 2009-02-26, Clarendon <> wrote:
    >> Hi. This must be a simple command but I just can't find it in the
    >> Phthon manual. How do I delete all items with a certain condition from
    >> a list? For instance: > L=['a', 'b', 'c', 'a']
    >> I want to delete all 'a's from the list. > But if L.remove('a')
    >> only deletes the first 'a'. How do you delete all 'a's?

    >
    > L2 = list(set(L))
    >
    > works for me...


    For a very strange definition of "works":

    py> L = ['a','b','c','a','j','b','z','b','a']
    py> L2
    ['a', 'c', 'b', 'z', 'j']
    py> L2 = list(set(L))
    py> L2
    ['a', 'c', 'b', 'z', 'j']

    I still see an 'a', there are things missing, and the order is totally
    lost.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Feb 27, 2009
    #12
  13. Clarendon

    Steve Holden Guest

    Peter Billam wrote:
    > On 2009-02-26, Clarendon <> wrote:
    >> Hi. This must be a simple command but I just can't find it in the
    >> Phthon manual. How do I delete all items with a certain condition from
    >> a list? For instance: > L=['a', 'b', 'c', 'a']
    >> I want to delete all 'a's from the list. > But if L.remove('a')
    >> only deletes the first 'a'. How do you delete all 'a's?

    >
    > L2 = list(set(L))
    >
    > works for me...


    I have to wonder for what value of "works" this works.

    The simplest problem is it doesn't remove all the "a"s.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Feb 27, 2009
    #13
  14. En Thu, 26 Feb 2009 22:18:18 -0200, Chris Rebert <>
    escribió:
    > On Thu, Feb 26, 2009 at 4:08 PM, Peter Billam <>
    > wrote:
    >> On 2009-02-26, Clarendon <> wrote:
    >>> Hi. This must be a simple command but I just can't find it in the
    >>> Phthon manual. How do I delete all items with a certain condition from
    >>> a list? For instance: > L=['a', 'b', 'c', 'a']
    >>> I want to delete all 'a's from the list.  > But if L.remove('a')
    >>> only deletes the first 'a'.  How do you delete all 'a's?

    >>
    >> L2 = list(set(L))
    >>
    >> works for me...

    >
    > A. That doesn't by itself remove all the 'a's, although it does remove
    > all but 1 'a'
    > B. That also removes all but one instance of *everything* in the list,
    > not just 'a'
    > C. That is lossy in that it completely loses the ordering of the
    > original list


    I said the very same things!
    There are other issues too, like this only works with hashable objects,
    but I've chosen exactly the same remarks and exactly in the same order!
    What a coincidence...

    --
    Gabriel Genellina
     
    Gabriel Genellina, Feb 27, 2009
    #14
  15. Clarendon

    Chris Rebert Guest

    On Thu, Feb 26, 2009 at 4:37 PM, Gabriel Genellina
    <> wrote:
    > En Thu, 26 Feb 2009 22:18:18 -0200, Chris Rebert <>
    > escribió:
    >>
    >> On Thu, Feb 26, 2009 at 4:08 PM, Peter Billam <>
    >> wrote:
    >>>
    >>> On 2009-02-26, Clarendon <> wrote:
    >>>>
    >>>> Hi. This must be a simple command but I just can't find it in the
    >>>> Phthon manual. How do I delete all items with a certain condition from
    >>>> a list? For instance: > L=['a', 'b', 'c', 'a']
    >>>> I want to delete all 'a's from the list.  > But if L.remove('a')
    >>>> only deletes the first 'a'.  How do you delete all 'a's?
    >>>
    >>> L2 = list(set(L))
    >>>
    >>> works for me...

    >>
    >> A. That doesn't by itself remove all the 'a's, although it does remove
    >> all but 1 'a'
    >> B. That also removes all but one instance of *everything* in the list,
    >> not just 'a'
    >> C. That is lossy in that it completely loses the ordering of the original
    >> list

    >
    > I said the very same things!
    > There are other issues too, like this only works with hashable objects, but
    > I've chosen exactly the same remarks and exactly in the same order! What a
    > coincidence...


    Indeed, USENET and mail<->news lags doth create interesting situations
    sometimes.

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Feb 27, 2009
    #15
  16. Clarendon

    odeits Guest

    On Feb 26, 3:05 am, Clarendon <> wrote:
    > Hi. This must be a simple command but I just can't find it in the
    > Phthon manual. How do I delete all items with a certain condition from
    > a list? For instance:
    >
    > L=['a', 'b', 'c', 'a']
    >
    > I want to delete all 'a's from the list.
    > But if L.remove('a') only deletes the first 'a'.
    >
    > How do you delete all 'a's?
    > I would really appreciate your help.
    >
    > Thanks.


    while 'a' in L:
    L.remove('a')

    not the most efficient but it works
     
    odeits, Feb 27, 2009
    #16
  17. Clarendon

    Chris Rebert Guest

    On Thu, Feb 26, 2009 at 10:26 PM, odeits <> wrote:
    > On Feb 26, 3:05 am, Clarendon <> wrote:
    >> Hi. This must be a simple command but I just can't find it in the
    >> Phthon manual. How do I delete all items with a certain condition from
    >> a list? For instance:
    >>
    >> L=['a', 'b', 'c', 'a']
    >>
    >> I want to delete all 'a's from the list.
    >> But if L.remove('a') only deletes the first 'a'.
    >>
    >> How do you delete all 'a's?
    >> I would really appreciate your help.
    >>
    >> Thanks.

    >
    > while 'a' in L:
    >   L.remove('a')
    >
    > not the most efficient but it works


    "Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M
    = number of 'a's in L], versus just N. And that's not even accounting
    for all the extra element movement done by .remove()

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Feb 27, 2009
    #17
  18. Clarendon

    Steve Holden Guest

    Chris Rebert wrote:
    > On Thu, Feb 26, 2009 at 10:26 PM, odeits <> wrote:

    [...]
    >> while 'a' in L:
    >> L.remove('a')
    >>
    >> not the most efficient but it works

    >
    > "Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M
    > = number of 'a's in L], versus just N. And that's not even accounting
    > for all the extra element movement done by .remove()
    >

    Surely 2*O(M*N) is O(M*N) since constant factors are irrelevant in
    assessing performance order?

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Feb 27, 2009
    #18
  19. Clarendon

    Chris Rebert Guest

    On Fri, Feb 27, 2009 at 5:10 AM, Steve Holden <> wrote:
    > Chris Rebert wrote:
    >> On Thu, Feb 26, 2009 at 10:26 PM, odeits <> wrote:

    > [...]
    >>> while 'a' in L:
    >>>   L.remove('a')
    >>>
    >>> not the most efficient but it works

    >>
    >> "Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M
    >> = number of 'a's in L], versus just N. And that's not even accounting
    >> for all the extra element movement done by .remove()
    >>

    > Surely 2*O(M*N) is O(M*N) since constant factors are irrelevant in
    > assessing performance order?


    Obviously that equivalence is true, but in this case I'm emphasizing
    that it's even worse than that when constant factors are taken into
    account. Big-O is nice in the abstract, but in the real-world those
    constant factors can matter.

    In pure big-O, it is indeed O(M*N) vs. O(N)
    Including constant factors, the performance is roughly 2*M*N*X [X =
    overhead of remove()] vs. N, which makes the badness of the algorithm
    all the more apparent.

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Feb 27, 2009
    #19
  20. Clarendon

    Clarendon Guest

    Thank you very much for all your replies. I actually used the while
    loop as the data is not large. But I was looking for a simpler, built
    in command, something like L.remove('a', all) or L.removeall('a').
    Python has re.findall function, but why not removeall, so users have
    to make up long lines of expression?
     
    Clarendon, Feb 28, 2009
    #20
    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. Gilles Kuhn
    Replies:
    0
    Views:
    443
    Gilles Kuhn
    Sep 15, 2003
  2. James Aguilar
    Replies:
    8
    Views:
    3,535
    Kristo
    Mar 22, 2005
  3. Tina Li
    Replies:
    0
    Views:
    304
    Tina Li
    Sep 18, 2003
  4. Anjan Bhowmik
    Replies:
    1
    Views:
    478
    Misbah Arefin
    Feb 14, 2008
  5. harry

    Delete all items from Drop Down?

    harry, Jun 16, 2004, in forum: Javascript
    Replies:
    7
    Views:
    89
    Grant Wagner
    Jun 17, 2004
Loading...

Share This Page