removing all instances of a certain value from a list

BJörn Lindqvist bjourne at gmail.com
Wed Mar 19 23:35:31 CET 2008


On Wed, Mar 19, 2008 at 10:28 PM, Lee Sander <lesande at gmail.com> wrote:
> 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.1999999999999993, 2.2999999999999998]

-- 
mvh Björn



More information about the Python-list mailing list