Ideas about optional filter on for loop statements

Duncan Booth me at privacy.net
Wed May 12 04:40:54 EDT 2004


bluczkie at andrew.cmu.edu (Brian L.) wrote in 
news:c186c44d.0405110926.5eead044 at posting.google.com:

> for x in l if x is not None:
>     ...
> 
> is certainly cleaner and easier to read. This clause should of course
> be optional
> 
> Thoughts? 

IMHO:

   for x in l:
       if x is not None:

is cleaner and easier to read than forcing everything onto one line. In 
most cases the names or expressions would be longer (and hopefully 
meaningful), so you would want to split the 'for' and 'if' onto two 
separate lines anyway.

Besides, these days you can write a generator trivially, so just put the 
condition inside a generator, invent a meaningful name for it and then you 
don't have to return None as a value at all.




More information about the Python-list mailing list