3 ms·
This does not answer my question, so I checked -- comprehensions simply do not accept the `else` clause: >>> [a for a in range(10) if True else range(2)]
by BerislavLopac 8y ago
This does not answer my question, so I checked -- comprehensions simply do not accept the `else` clause:
>>> [a for a in range(10) if True else range(2)]
File "<stdin>", line 1
[a for a in range(10) if True else range(2)]
^
SyntaxError: invalid syntax
And this is the argument why I can't have my wish, because the standard `for` loops have always accepted `if else`, so it would be a backward incompatible change.
That said, I have another idea: an update to the comprehension syntax which would omitting duplication of variables, using a new "for in" construct. For example, this line:
(x for x in range(100) if x%2)
...could be written as:
(x for in range(100) if x%2)
Just an idea... :D