Dear Robert, Thanks a lot for your help, this is exactly what I wanted. I still don't get why it is necessary to import a special function to perform such a standard thing. "import copy" has the affect that suddenly I cannot do copy(L) any more, but I have to do copy.copy(L) instead. Since copy(L) does not do what I would expect anyway, I will avoid it. Frankly, as intuitive as Python is in other respects, this behaviour blows my mind.
Cheers Stan On Aug 17, 6:19 pm, Robert Bradshaw <rober...@math.washington.edu> wrote: > 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