On Mon, 10 Jan 2005 09:13:17 -0700, Steven Bethard <[EMAIL PROTECTED]> wrote: > Luis M. Gonzalez wrote: > > It's me wrote: > >>> 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 ... > >> Or are we trying to write a book on "Puzzles in Python"? > > > > Once you get used to list comprehensions (and it doesn't take long), > > they are a more concise and compact way to express these operations. > > After looking the two suggestions over a couple of times, I'm still > undecided as to which one is more readable for me. The problem is not > the list comprehensions (which I love and use extensively). The problem > is the odd syntax that has to be used for an if/then/else expression in > Python.
They're both pretty unreadable, IMHO. Why not just factor out the if/then/else function like this: .def plusMinusTwo(i): . if i%2 == 0: . return i-2 . else: . return i+2 . .z = [plusMinusTwo(i) for i in range(10)] Then you can add whatever you like into the function. Anthony -- ----------------------------------------------------- HyPEraCtiVE? HeY, WhO aRE YoU cALliNg HypERaCtIve?! [EMAIL PROTECTED] ----------------------------------------------------- -- http://mail.python.org/mailman/listinfo/python-list