Modifying values in a list

Tim Williams (gmail) tdwdotnet at gmail.com
Thu Dec 29 11:58:29 EST 2005


On 29 Dec 2005 08:43:17 -0800, tron.thomas at verizon.net <
tron.thomas at verizon.net> wrote:
>
> The following code:
>
> numbers = [1, 2, 3]
> for value in numbers:
>         value *= 2
> print numbers


Above,  you are modifying "value", not the item in the list


>>> numbers = [1, 2, 3]
>>> for x in range(len(numbers)):
...     numbers[x] = numbers[x]*2
>>> numbers
[2, 4, 6]

HTH :)

--

Tim Williams
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051229/5871d436/attachment.html>


More information about the Python-list mailing list