Removing null items

Preston Landers prestonlanders at my-deja.com
Tue Nov 30 16:22:12 EST 1999


While not especially ingenious, this method does modify the list in
place, and is different.  Well, only in the sense that it is basically
the equivelent inverse of what you did. ;-)

def remove_false_from_list(the_list):
   for item in the_list:
      if not item:
         the_list.remove(item)
   return the_list

---Preston

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.
>

--
|| Preston Landers <prestonlanders at my-deja.com> ||


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




More information about the Python-list mailing list