x = y

Jay O'Connor joconnor at cybermesa.com
Tue Feb 6 16:41:35 EST 2001


On Tue, 06 Feb 2001 21:13:13 GMT, "José Luis Gallego"
<gallegoj at ono.com> wrote:

>Hi all,
>
>I want to create a list x from a list y:
>
>y = [[1,2,3],[4,5,6]]
>x = y
>
>How do I change individual elements of x without changing y?


Make a copy of the list

	x  = list (y)

x will contain the same elements as y, but will be a seperate list, so
you can update either indepently

>>> y = [[1,2,3],[4,5,6]]
>>> x = list (y)
>>> x
[[1, 2, 3], [4, 5, 6]]
>>> y
[[1, 2, 3], [4, 5, 6]]
>>> x [0] = "hi there"
>>> x
['hi there', [4, 5, 6]]
>>> y
[[1, 2, 3], [4, 5, 6]]
>>> 

Take care,


Jay O'Connor
joconnor at cybermesa.com
http://www.cybermesa.com/~joconnor

Python Language Discussion Forum - http://pub1.ezboard.com/fobjectorienteddevelopmentpython

"God himself plays on the bass strings first, when he tunes the soul"



More information about the Python-list mailing list