Re: Modifying values in a list

2005-12-29 Thread Paul McGuire
As others have already posted, changing the value of 'value' has nothing to do with the list variable 'numbers'. To modify the list in place, you need to access its members by index. The following code does this: numbers = [1,2,3] for i in range(len(numbers)): numbers[i] *= 2 print numbers

Re: Modifying values in a list

2005-12-29 Thread Tim Williams (gmail)
On 29 Dec 2005 08:43:17 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED] > wrote:The following code:numbers = [1, 2, 3]for value in numbers: value *= 2print numbers Above,  you are modifying "value", not the item in the list >>> numbers = [1, 2, 3] >>> for x in range(len(numbers)): ...     num

Re: Modifying values in a list

2005-12-29 Thread Carsten Haese
On Thu, 2005-12-29 at 11:43, [EMAIL PROTECTED] wrote: > The following code: > > numbers = [1, 2, 3] > for value in numbers: > value *= 2 > print numbers > > results in the following output: > [1, 2, 3] > > The intent of the code was to produce this output: > [2, 4, 6] > > What is the reas

Re: Modifying values in a list

2005-12-29 Thread Brian van den Broek
[EMAIL PROTECTED] said unto the world upon 29/12/05 10:43 AM: > The following code: > > numbers = [1, 2, 3] > for value in numbers: > value *= 2 > print numbers > > results in the following output: > [1, 2, 3] > > The intent of the code was to produce this output: > [2, 4, 6] > > What is

Re: Modifying values in a list

2005-12-29 Thread Björn Lindström
[EMAIL PROTECTED] writes: > The following code: > > numbers = [1, 2, 3] > for value in numbers: > value *= 2 > print numbers > > results in the following output: > [1, 2, 3] > > The intent of the code was to produce this output: > [2, 4, 6] > > What is the reason for the output produced? > W

Modifying values in a list

2005-12-29 Thread tron . thomas
The following code: numbers = [1, 2, 3] for value in numbers: value *= 2 print numbers results in the following output: [1, 2, 3] The intent of the code was to produce this output: [2, 4, 6] What is the reason for the output produced? What code should be used to obtain the desired outpu