else in list comp

Greg Ewing (using news.cis.dfn.de) me at privacy.net
Wed Jan 29 21:01:21 EST 2003


Cliff Wells wrote:

> What I thought of first:
> 
> ['' for i in l if i is None else i]
> 
> Any thoughts on something like this?


I don't think you've thought this through properly. Consider
that a list comp can have multiple if and/or for clauses
interspersed. How would your proposed feature work in the
general case?

Also, you need to understand that an if clause in a list
comp decides *whether* to include an item in the list;
it doesn't decide *what* to include in the list. Given
that, I can't see that attaching an else clause to it
makes any sense.

Greg





More information about the Python-list mailing list