Previous Up Next

5.4.4  Copying lists: copy

If you enter

list1:= [1,2,3]

and then

list2:= list1

then list1 and list2 will be equal to the same list, not simply two lists with the same elements. In particular, if you change (by reference) the value of an element of list1, then the change will also be reflected in list2. For example, if you enter

list1[1] =< 5

then both list1 and list2 will be equal to [1,5,3].

The copy command creates a copy of a list (or vector or matrix) which is equal to the original list, but distinct from it. For example, if you enter

list1:= [1,2,3]

and then

list2:= copy(list1)

then list1 and list2 will both be [1,2,3], but now if you enter

list1[1] =< 5

then list1 will be equal to [1,5,3] but list2 will still be [1,2,3].


Previous Up Next