else condition in list comprehension

It's me itsme at yahoo.com
Mon Jan 10 01:19:35 EST 2005


> z = [i + (2, -2)[i % 2] for i in range(10)]

But then why would you want to use such feature?  Wouldn't that make the
code much harder to understand then simply:

z=[]
for i in range(10):
    if  i%2:
        z.append(i-2)
    else:
        z.append(i+2)

Or are we trying to write a book on "Puzzles in Python"?





More information about the Python-list mailing list