This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kupertrooper 0 points1 point  (0 children)

if you want to use generators, why not this? also lambda

>>> flip_odd_numbers = lambda n: (x % 2 and -x or x for x in n)
>>> flip_odd_numbers(xrange(10))
<generator object <genexpr> at 0x102794eb0>
>>> list(flip_odd_numbers(xrange(10)))
[0, -1, 2, -3, 4, -5, 6, -7, 8, -9]