Extended List Comprehension

Paul Rubin http
Fri Jan 20 16:09:38 EST 2006


James Stroud <jstroud at ucla.edu> writes:
> py> [x for x in '1234' if x%2 else 'even']
> [1, 'even', 3, 'even']
> 
> I'm guessing this has been suggested before?

You could (in 2.5) use:

   [(x if x%2 else 'even') for x in '1234']



More information about the Python-list mailing list