[Python-Dev] PEP-xxx: Unification of for statement and list-comp syntax

Greg Ewing greg.ewing at canterbury.ac.nz
Mon May 22 08:44:47 CEST 2006


Heiko Wundram wrote:
> Am Montag 22 Mai 2006 01:59 schrieb Josiah Carlson:
> > Not everyone finds list comprehensions easy to read.
> 
> Why has Python added list-comprehensions, then? (or at least, why has Python 
> added the 'if'-expression to list-comprehensions if they're hard to read?

LCs are useful because they're expressions rather than statements.
Being expressions, they need if-clauses in order to be able to
conditionally include items in the list. LCs with if-clauses don't
*have* to be hard to read; you just need to lay them out on separate
lines, as you would do when writing nested statements.

With a for-statement, there is no need for if-clauses, since you
can use a nested if-statement to get the same effect. The only
possible reason for wanting an if-clause would be so that you
can write it on the same line, which reduces readability for no
corresponding benefit.

--
Greg


More information about the Python-Dev mailing list