removing all instances of a certain value from a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lee Sander

    removing all instances of a certain value from a list

    Hi,
    I have a float array ( eg [-1.3, 1.22, 9.2, None, 2.3] ) but there are
    many missing vlaues which are represented as None. I would like to
    remove all such instances in one go.
    There is a remove function but it removes only the first instance, is
    there a delete/remove all function?
    thanks
  • =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

    #2
    Re: removing all instances of a certain value from a list

    On Wed, Mar 19, 2008 at 10:28 PM, Lee Sander <lesande@gmail. comwrote:
    Hi,
    I have a float array ( eg [-1.3, 1.22, 9.2, None, 2.3] ) but there are
    many missing vlaues which are represented as None. I would like to
    remove all such instances in one go.
    There is a remove function but it removes only the first instance, is
    there a delete/remove all function?
    thanks
    If it is ok to copy the list instead of mutating it, use a list comprehension:
    >>L = [-1.3, 1.22, 9.2, None, 2.3]
    >>[x for x in L if x is not None]
    [-1.3, 1.22, 9.1999999999999 993, 2.2999999999999 998]

    --
    mvh Björn

    Comment

    • Hrvoje Niksic

      #3
      Re: removing all instances of a certain value from a list

      Lee Sander <lesande@gmail. comwrites:
      Hi,
      I have a float array ( eg [-1.3, 1.22, 9.2, None, 2.3] ) but there are
      many missing vlaues which are represented as None. I would like to
      remove all such instances in one go.
      There is a remove function but it removes only the first instance, is
      there a delete/remove all function?
      No, but you can easily simulate it using, for example:

      lst[:] = (el for el in lst if el is not None)

      Comment

      • r.grimm@science-computing.de

        #4
        Re: removing all instances of a certain value from a list

        On Mar 19, 11:28 pm, Lee Sander <lesa...@gmail. comwrote:
        Hi,
        I have a float array ( eg [-1.3, 1.22, 9.2, None, 2.3] ) but there are
        many missing vlaues which are represented as None. I would like to
        remove all such instances in one go.
        There is a remove function but it removes only the first instance, is
        there a delete/remove all function?
        thanks
        You can also do it with the filter function.
        >>a= [-1.3, 1.22, 9.2, None, 2.3]
        >>a=filter ( lambda b: b != None, a)
        >>print a
        [-1.3, 1.22, 9.1999999999999 993, 2.2999999999999 998]

        Greetings Rainer

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: removing all instances of a certain value from a list

          r.gr...@science-computing.de:
          >a=filter ( lambda b: b != None, a)
          With None it's better to use is/not is instead of ==/!=

          Bye,
          bearophile

          Comment

          Working...