« Return to Thread: list comprehension problem

Re: list comprehension problem

by Bruno Desthuilliers-6 :: Rate this Message:

Reply to Author | View in Thread

Falcolas a écrit :
(snip)
 >
> 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:
       whatever.append(s)

As far as I'm concerned, I have a clear preference for the first
version, but well, YMMV...
--
http://mail.python.org/mailman/listinfo/python-list

 « Return to Thread: list comprehension problem