Better solution

Michael Hudson mwh at
Wed Aug 21 10:24:47 CEST 2002

"Bo M. Maryniuck" <b.maryniuk at> writes:

> On Tuesday 20 August 2002 17:39, Michael Hudson wrote:
> > >   [x for x in lst if x]
> > If you want to mutate the list, I'd say:
> > lst[:] = filter(None, lst)
> > is better than the monstrosity above.
> Hmm... why the mostrosity? Is this example code is too ugly?
> >>> [x for x in ['', 'a', 'b','!', 'c'] if x not in ['!', '']]       
> ['a', 'b', 'c']

Oh, not *that* monstrosity, this one:

lst = ['', 'a', '', 'b', 'c', '', 'd']
map(lambda z:lst.pop(lst.index('')), range(0, lst.count('')))

I realise what I wrote was a tad confusing.

> Also what is [:]?

A slice.


  Reading Slashdot can [...] often be worse than useless, especially
  to young and budding programmers: it can give you exactly the wrong
  idea about the technical issues it raises.

More information about the Python-list mailing list