Modifying values in a list

T

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 output?
 
G

Guest

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 output?

How about this?

numbers = [1, 2, 3]
print [x * 2 for x in numbers]
 
B

Brian van den Broek

(e-mail address removed) 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 the reason for the output produced?
What code should be used to obtain the desired output?

value is bound in the for loop, but none of that affects the list to
which numbers is bound.

One way:

IDLE 1.1.2
>>> numbers = [1, 2, 3]
>>> numbers = [x * 2 for x in numbers]
>>> print numbers [2, 4, 6]
>>>

HTH,

Brian vdB
 
C

Carsten Haese

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 output?

1) Read http://www.effbot.org/zone/python-objects.htm and reread it
until you understand why your code doesn't work.

2) Use a list comprehension:
numbers = [ value*2 for value in numbers ]

-Carsten
 
P

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 *= 2
print numbers

But this is how a C or Java programmer would approach this task, and is
not very Pythonic.

A bit better is to use enumerate (avoids the ugly range(len(blah))
looping):

numbers = [1,2,3]
for i,val in enumerate(numbers):
numbers = val*2

This also modifies the list in place, so that if you are modifying a
very long (and I mean really quite long - long enough to question why
you are doing this in the first place) list, then this approach avoids
making two lists, one with the old value and one with the new.

But the most Pythonic is to use one of the list comprehension forms
already posted, such as:

numbers = [ x*2 for x in numbers ]

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,373
Members
48,065
Latest member
SusannaSpe

Latest Threads

Top