On Wed, Aug 17, 2011 at 9:09 AM, Stan Schymanski <schym...@gmail.com> wrote:
> Dear all,
>
> This has been driving me mad. According to the python documentation,
> you can modify a copy of a list without modifying the original using
> the following code:
>
> sage: L = []
> sage: M = L[:] # create a copy
> sage: # modify L only
> sage: L.append(1)
> sage: M
> []
>
> Now, I want to do the same with a nested list, but I do not manage to
> unlink the two. See the example below, where I also tried copy(L) to
> no avail. I hope that someone can help. Thanks already!

A "copy" of a list is a new list containing exactly the same elements
as the original list. It sounds like what you want here is

sage: import copy
sage: L = [range(k) for k in range(5)]
sage: M = copy.deepcopy(L)
sage: M[0].append('something')
sage: M
[['something'], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]
sage: L
[[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]

- Robert

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to