Extended List Comprehension
Paul Rubin
http
Fri Jan 20 16:43:06 EST 2006
Tim Chase <python.list at tim.thechases.com> writes:
> > You could (in 2.5) use:
> > [(x if x%2 else 'even') for x in '1234']
>
> This failed on multiple levels in 2.3.5 (the "if" syntax is
> unrecognized,
Yes, that syntax is new to 2.5.
> However, this worked in my 2.3.5:
> [((x%2 == 0 and 'even') or x) for x in [-2,-1,0,1,2,3,4]]
> It uses the fairly defacto short-circuit-evaluation usage to coin a
> Python trinary operator like the C/C++ colon/question-mark pair of
> operators.
Yeah, you could do that, but there's various drawbacks, hence the new
syntax being introduced in 2.5.
More information about the Python-list
mailing list