Removing null items

aj10 at my-deja.com aj10 at my-deja.com
Tue Nov 30 18:36:02 EST 1999


In article <821761$s8u$1 at nnrp1.deja.com>,
  sragsdale at my-deja.com wrote:
> So I've got a very simple desire: to remove all false (I.E. None,
0, '',
> []) items from a list.
>
> As it is I've got this function which works fine, but it offends my
> sight.  Is there any better (or at least different) way to do this?
>
> #################################################################
> # return non-null items from the first list
> def foo(lst):
>     rlist = []
>     for i in lst:
>         if i:
>             rlist.append(i)
>     return rlist
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
This has been answered just recently. Repeating it in the right place
with an example.
--
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> a=[None, 'aj', 'mk', 10, {1:2, 'a':10}, {}, None, []]
>>> a
[None, 'aj', 'mk', 10, {1: 2, 'a': 10}, {}, None, []]
>>> filter(None, a)
['aj', 'mk', 10, {1: 2, 'a': 10}]
>>>
--
Thx
-aj


Sent via Deja.com http://www.deja.com/
Before you buy.




More information about the Python-list mailing list