Re: Modifying every alternate element of a sequence

2006-11-28 Thread bearophileHUGS
Leo Kislov: > input[1::2] = [-item for item in input[1::2]] > If you don't want to do it in-place, just make a copy: > wanted = input[:] > wanted[1::2] = [-item for item in wanted[1::2]] Very nice solution. I have tried few versions like: from itertools import imap, islice from operator import neg

Re: Modifying every alternate element of a sequence

2006-11-28 Thread Leo Kislov
[EMAIL PROTECTED] wrote: > Wow, I was in fact searching for this syntax in the python tutorial. It > is missing there. > Is there a reference page which documents all possible list > comprehensions. There is actually only two forms of list comprehensions: http://docs.python.org/ref/lists.html [bl

Re: Modifying every alternate element of a sequence

2006-11-28 Thread Steven D'Aprano
On Tue, 28 Nov 2006 02:38:09 -0800, [EMAIL PROTECTED] wrote: > I have a list of numbers and I want to build another list with every > second element multiplied by -1. > > input = [1,2,3,4,5,6] > wanted = [1,-2,3,-4,5,-6] > > I can implement it like this: > > input = range(3,12) > wanted = [] >

Re: Modifying every alternate element of a sequence

2006-11-28 Thread Roberto Bonvallet
[EMAIL PROTECTED] 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)

Re: Modifying every alternate element of a sequence

2006-11-28 Thread [EMAIL PROTECTED]
Wow, I was in fact searching for this syntax in the python tutorial. It is missing there. Is there a reference page which documents all possible list comprehensions. -- Suresh Leo Kislov wrote: > [EMAIL PROTECTED] wrote: > > I have a list of numbers and I want to build another list with every > >

Re: Modifying every alternate element of a sequence

2006-11-28 Thread Antoon Pardon
On 2006-11-28, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I have a list of numbers and I want to build another list with every > second element multiplied by -1. > > input = [1,2,3,4,5,6] > wanted = [1,-2,3,-4,5,-6] > > I can implement it like this: > > input = range(3,12) > wanted = [] > for (

Re: Modifying every alternate element of a sequence

2006-11-28 Thread Leo Kislov
[EMAIL PROTECTED] wrote: > I have a list of numbers and I want to build another list with every > second element multiplied by -1. > > input = [1,2,3,4,5,6] > wanted = [1,-2,3,-4,5,-6] > > I can implement it like this: > > input = range(3,12) > wanted = [] > for (i,v) in enumerate(input): > if

Re: Modifying every alternate element of a sequence

2006-11-28 Thread John Hicken
[EMAIL PROTECTED] wrote: > I have a list of numbers and I want to build another list with every > second element multiplied by -1. > > input = [1,2,3,4,5,6] > wanted = [1,-2,3,-4,5,-6] > > I can implement it like this: > > input = range(3,12) > wanted = [] > for (i,v) in enumerate(input): > if

Re: Modifying every alternate element of a sequence

2006-11-28 Thread Tim Chase
> I have a list of numbers and I want to build another list with every > second element multiplied by -1. > > input = [1,2,3,4,5,6] > wanted = [1,-2,3,-4,5,-6] > > I can implement it like this: > > input = range(3,12) > wanted = [] > for (i,v) in enumerate(input): > if i%2 == 0: > wa

Modifying every alternate element of a sequence

2006-11-28 Thread [EMAIL PROTECTED]
I have a list of numbers and I want to build another list with every second element multiplied by -1. input = [1,2,3,4,5,6] wanted = [1,-2,3,-4,5,-6] I can implement it like this: input = range(3,12) wanted = [] for (i,v) in enumerate(input): if i%2 == 0: wanted.append(v) else: