list comprehension problem

Krister Svanlund krister.svanlund at gmail.com
Mon Nov 2 17:31:21 EST 2009


On Mon, Nov 2, 2009 at 10:11 PM, Aahz <aahz at pythoncraft.com> wrote:
> In article <7589e0a1-98b2-4df4-bc76-5d4c10194fde at f20g2000prn.googlegroups.com>,
> Falcolas  <garrickp at gmail.com> wrote:
>>
>>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()]
>
> This breaks if s might be None

If you don't want Nones in your list just make a check for it...
[s.strip() for s in hosts if s is not None and s.strip()]



More information about the Python-list mailing list