pythonian way

Moshe Zadka moshez at math.huji.ac.il
Fri Feb 25 05:16:57 EST 2000


On Fri, 25 Feb 2000, Alexander Williams wrote:

> >Yeah, but I need to remove empty lines as well...
> 
> Try:
> 
>  >>> A = filter(None, H.readlines())
> 
> filter() with None as the function to use as the test for elements
> removes any that test as False intrinsically; since the empty line is
> always False, it returns a list with only strings with length.Bingo.

H.readlines() will never contain *truly* empty lines. The original poster
meant "lines with nothing but the newline".

Here's a version that works:

A = filter(lambda x: x != '\n', H.readlines())

Or, to keep the style of the original post:

A = filter(lambda x: x <> '\n', H.readlines())

now-skip-will-say-<>-is-deprecated-ly y'rs, Z.
--
Moshe Zadka <mzadka at geocities.com>. 
INTERNET: Learn what you know.
Share what you don't.





More information about the Python-list mailing list