Proposed PEP for a Conditional Expression

Rainer Deyke root at
Thu Sep 13 07:31:44 CEST 2001

"Greg Ewing" <greg at> wrote in message
news:3BA00239.DECE6412 at
> Bernhard Herzog <bh at> wrote:
> >         <expr> if <cond> else <expr>
> I find myself liking this. To me,
>   x = a if something else b
> reads much more smoothly than
>   x = if something then a else b
> Rainer Deyke wrote:
> >
> > However, it would not fit well with list comprehensions that have a
> >
> > [<expr> if <cond> for i in range(5)] # Syntax error
> > [<expr> for i in range(5) if <cond> else <expr>] # Syntax error
> >                                          # and ambiguous intent
> This can be remedied: give the conditional expression
> a precedence lower than any existing operator (which I
> think is a good idea anyway), and exclude this level of
> precedence from the iterated-over expression of a for loop.

I was actually considering the following interpretations:

[(<expr> if <cond> else <expr>) for i in range(5)]
[(<expr> for i in range(5)) if <cond> else <expr>]

It seems you were thinking of one that never even occurred to me:

[<expr> for i in (range(5) if <cond> else <expr>)]

Rainer Deyke (root at
Shareware computer games           - 
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor

More information about the Python-list mailing list