Re: Modifying every alternate element of a sequence



jm.suresh@xxxxxxxxxxxxxxxxx wrote:
I have a list of numbers and I want to build another list with every
second element multiplied by -1.
[...]
But is there any other better way to do this.

I think the best way is the one that uses slices, as somebody suggested
in this thread. This is another (worse) way, just for fun:

>>> from itertools import cycle
>>> input = [1, 2, 3, 4, 5, 6]
>>> wanted = [x * sign for x, sign in zip(input, cycle([1, -1]))]
>>> wanted
[1, -2, 3, -4, 5, -6]

Cheers,
--
Roberto Bonvallet
.