Modifying values in a list

Björn Lindström bkhl at stp.lingfil.uu.se
Thu Dec 29 11:51:25 EST 2005


tron.thomas at verizon.net 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?
> What code should be used to obtain the desired output?

How about this?

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

-- 
Björn Lindström <bkhl at stp.lingfil.uu.se>
Student of computational linguistics, Uppsala University, Sweden



More information about the Python-list mailing list