[Tutor] "if clause" in list comprehensions.

Kent Johnson kent37 at tds.net
Wed Oct 21 12:22:19 CEST 2009

On Wed, Oct 21, 2009 at 3:30 AM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
> Is the if-else (esp. 'else') in a list comprehension specific for Python 3.x? Or did I miss something?

List comps have always allowed a condition since they were introduced:
  [ <expression> for <variable> in <sequence> if <condition> ]

This is the form of the original question.

Conditional expressions of the form
  <value> if <condition> else <alternate value>
are relatively new. The answers to the original question use this as
part of the <expression> term of the list comp. So they are not using
any new list comp syntax, they are using new expression syntax.


More information about the Tutor mailing list