[Python-ideas] Ternary operators in list comprehensions
jhihn at gmx.com
Thu Oct 5 11:40:32 EDT 2017
>>> a = [1,2,3]
>>> [ x for x in a if x & 1]
>>> [ x for x in a if x & 1 else 'even']
File "<stdin>", line 1
[ x for x in a if x & 1 else 'even']
SyntaxError: invalid syntax
I expected [1, 'even', 3]
I would expect that the if expression would be able to provide alternative values through else.
The work around blows it out to:
l = 
for x in a:
Unless there is a better way?
More information about the Python-ideas