list comprehension problem

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Oct 29 17:17:54 CET 2009

Falcolas a écrit :
> I'd also recommend trying the following filter, since it is identical
> to what you're trying to do, and will probably catch some additional
> edge cases without any additional effort from you.
> [s.strip() for s in hosts if s.strip()]

The problem with this expression is that it calls str.strip two times... 
Sometimes, a more lispish approach is better:

    whatever = filter(None, map(str.strip, hosts))

or just a plain procedural loop:

   whatever = []
   for s in hosts:
     s = s.strip()
     if s:

As far as I'm concerned, I have a clear preference for the first 
version, but well, YMMV...

More information about the Python-list mailing list