ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 18 Nov 2018 10:05:50 -0600question about Python assignment of lists; meaning of equals sign; how to save a list?http://ask.sagemath.org/question/44324/question-about-python-assignment-of-lists-meaning-of-equals-sign-how-to-save-a-list/I have a list, then modify it, but want to save the old list to then modify in a different way.
I try to save the original using a different name and =, but it seems the two names are forever linked by the equals sign,
so the modification also changes the original. Why does this happen, and how do I deal with this?
Example:
sage: L1 = [1, 2]
sage: L2 = [3, 4]
sage: L3 = L1
sage: L1
[1, 2]
sage: L2
[3, 4]
sage: L3
[1, 2]
sage: L1.extend(L2)
sage: L1
[1, 2, 3, 4]
sage: L2
[3, 4]
sage: L3
[1, 2, 3, 4]
The same thing happens with "append".
Help please!!!
Note: this does not happen with variables with numerical (not list) values. For example:
sage: a = 4
sage: b = a
sage: a = 5
sage: a
5
sage: b
4
So it seems that the equals sign means two different things: for numbers it is an assignment;
for lists it is an identification. This is driving me crazy. I cannot find it explained anywhere (maybe because it is so "well-known"?)Sun, 18 Nov 2018 08:59:29 -0600http://ask.sagemath.org/question/44324/question-about-python-assignment-of-lists-meaning-of-equals-sign-how-to-save-a-list/Answer by al for <p>I have a list, then modify it, but want to save the old list to then modify in a different way.
I try to save the original using a different name and =, but it seems the two names are forever linked by the equals sign,
so the modification also changes the original. Why does this happen, and how do I deal with this?</p>
<p>Example:</p>
<pre><code>sage: L1 = [1, 2]
sage: L2 = [3, 4]
sage: L3 = L1
sage: L1
[1, 2]
sage: L2
[3, 4]
sage: L3
[1, 2]
sage: L1.extend(L2)
sage: L1
[1, 2, 3, 4]
sage: L2
[3, 4]
sage: L3
[1, 2, 3, 4]
</code></pre>
<p>The same thing happens with "append".</p>
<p>Help please!!!</p>
<p>Note: this does not happen with variables with numerical (not list) values. For example:</p>
<pre><code>sage: a = 4
sage: b = a
sage: a = 5
sage: a
5
sage: b
4
</code></pre>
<p>So it seems that the equals sign means two different things: for numbers it is an assignment;
for lists it is an identification. This is driving me crazy. I cannot find it explained anywhere (maybe because it is so "well-known"?)</p>
http://ask.sagemath.org/question/44324/question-about-python-assignment-of-lists-meaning-of-equals-sign-how-to-save-a-list/?answer=44327#post-id-44327I think I just found what the problem is: "This is really a matter of how Python handles assignments of lists."
Here is the solution:
sage: L1 = [1, 2]
sage: L2 = [3, 4]
sage: L1
[1, 2]
sage: L2
[3, 4]
sage: L1.extend(L2)
sage: L1
[1, 2, 3, 4]
sage: L2
[3, 4]
sage: L3 = L1[:]
sage: L3
[1, 2, 3, 4]
sage: L1.append(8)
sage: L1
[1, 2, 3, 4, 8]
sage: L3
[1, 2, 3, 4]
I found the explanation in @calc314's answer to [Ask Sage question 25998](https://ask.sagemath.org/question/25998) by @ikol.Sun, 18 Nov 2018 10:05:50 -0600http://ask.sagemath.org/question/44324/question-about-python-assignment-of-lists-meaning-of-equals-sign-how-to-save-a-list/?answer=44327#post-id-44327