<p>Welcome to Bioinformatics.</p>
<p>First of all, it should not be asked in this community since it is purely a question about python programming. I suggest that next time, please head to <a href="https://stackoverflow.com/">StackOverflow</a> as you will likely receive more help from there.</p>
<p>You also look new to programming. This is more of a logical question. Let's break it up to understand what is happening in your first example:</p>
<pre class="lang-py prettyprint-override"><code># you define your dictionary containing two keys along with their values.
dict_eg1 = {
             &quot;A&quot;:[1,2,3],    # Key 'A' with a value as a list [1,2,3]
             &quot;B&quot;:[11,22,33]  # Key 'B' with a value as a list [11,22,33]
}

# you now define a new variable 'val1' which is
# the value of key 'A' and that is the list [1,2,3]
val1=dict_eg1[&quot;A&quot;]

# as you have mentioned, doing the below command
# will append the value to the list of key 'A'
# in other words, you can also write it like so
# dict_eg1[&quot;A&quot;].append(100)
# that's why when you make a change to 'val1'
# the value of the key 'A' of 'dict_eg1' will change
val1.append (100)
</code></pre>
<p>As for the second example (I will skip the same steps):</p>
<pre class="lang-py prettyprint-override"><code># ...

# you store the value of key 'A' in 'val1'
val1=dict_eg1[&quot;A&quot;]

# however, in the below command, you actually
# !redefine! the value of 'val1' to a new list
# that is totally independent of your 'dict_eg1'
# In this case, 'dict_eg1' is not affected by
# any changes made to 'val1'
val1 = [1, 2, 3, 100]
</code></pre>
<p>Just to sum it up, when you use <code>val1=dict_eg1[&quot;A&quot;]</code>, imagine that you link the value of your key <code>A</code> to <code>val1</code> and any changes made to <code>val1</code> will affect <code>dict_eg1[&quot;A&quot;]</code>. However, <em><strong>redefining</strong></em> <code>val1</code> will disconnect that link.</p>

CC BY-SA 4.0
2022-05-19T02:46:01.673Z
Thank you. I am sorry that I posted this question in the inappropriate community.
Apppii092
CC BY-SA 4.0
2022-05-18T05:51:41.927Z
2022-05-18T05:58:21.460Z
2022-05-18T05:58:21.460Z
user324810
<p>I have a list named dict_eg1 whose values are lists.</p>
<pre><code>dict_eg1 ={
    &quot;A&quot;:[1,2,3],
    &quot;B&quot;:[11,22,33],}
val1=dict_eg1[&quot;A&quot;]
val1.append (100)#modify the list by using append.
print (dict_eg1)
</code></pre>
<p>{'A': [1, 2, 3, 100], 'B': [11, 22, 33]}</p>
<p>In this case, dict_eg1 changed.</p>
<p>However,</p>
<pre><code>dict_eg1 ={
    &quot;A&quot;:[1,2,3],
    &quot;B&quot;:[11,22,33],}
val1=dict_eg1[&quot;A&quot;]
val1 = [1, 2, 3, 100]#modify the list without using append.
dict_eg1
</code></pre>
<p>{'A': [1, 2, 3], 'B': [11, 22, 33]}
In this case, dict_eg1 did not change.</p>
<p>Can anyone tell me why these two results were different??</p>

CC BY-SA 4.0
2022-05-18T09:06:58.663Z
I’m voting to close this question because it is a pure python question and not related to bioinformatics.
terdon
CC BY-SA 4.0
2022-05-18T18:36:29.190Z
To sum up, .`append()` modifies the list in-place, while assignment with `=` creates a new list entirely
Chris_Rands
<python>
Python; Question about the append (modifying lists): why modifying a list with append changes others?
# Python; Question about the append (modifying lists): why modifying a list with append changes others?

(Asked by: username_0 on 2022-05-18)

I have a list named dict_eg1 whose values are lists.
dict_eg1 ={
    "A":[1,2,3],
    "B":[11,22,33],}
val1=dict_eg1["A"]
val1.append (100)#modify the list by using append.
print (dict_eg1)

{'A': [1, 2, 3, 100], 'B': [11, 22, 33]}
In this case, dict_eg1 changed.
However,
dict_eg1 ={
    "A":[1,2,3],
    "B":[11,22,33],}
val1=dict_eg1["A"]
val1 = [1, 2, 3, 100]#modify the list without using append.
dict_eg1

{'A': [1, 2, 3], 'B': [11, 22, 33]}
In this case, dict_eg1 did not change.
Can anyone tell me why these two results were different??

**Tags:** python
> **Comments:**
> **username_1**: I’m voting to close this question because it is a pure python question and not related to bioinformatics.  
> **username_2**: To sum up, .`append()` modifies the list in-place, while assignment with `=` creates a new list entirely  

---

## 1 answers

### Answer by username_3 on 2022-05-18. Score: 1 | Accepted answer

Welcome to Bioinformatics.
First of all, it should not be asked in this community since it is purely a question about python programming. I suggest that next time, please head to StackOverflow as you will likely receive more help from there.
You also look new to programming. This is more of a logical question. Let's break it up to understand what is happening in your first example:
# you define your dictionary containing two keys along with their values.
dict_eg1 = {
             "A":[1,2,3],    # Key 'A' with a value as a list [1,2,3]
             "B":[11,22,33]  # Key 'B' with a value as a list [11,22,33]
}

# you now define a new variable 'val1' which is
# the value of key 'A' and that is the list [1,2,3]
val1=dict_eg1["A"]

# as you have mentioned, doing the below command
# will append the value to the list of key 'A'
# in other words, you can also write it like so
# dict_eg1["A"].append(100)
# that's why when you make a change to 'val1'
# the value of the key 'A' of 'dict_eg1' will change
val1.append (100)

As for the second example (I will skip the same steps):
# ...

# you store the value of key 'A' in 'val1'
val1=dict_eg1["A"]

# however, in the below command, you actually
# !redefine! the value of 'val1' to a new list
# that is totally independent of your 'dict_eg1'
# In this case, 'dict_eg1' is not affected by
# any changes made to 'val1'
val1 = [1, 2, 3, 100]

Just to sum it up, when you use val1=dict_eg1["A"], imagine that you link the value of your key A to val1 and any changes made to val1 will affect dict_eg1["A"]. However, redefining val1 will disconnect that link.

> **Comments:**
> **username_0**: Thank you. I am sorry that I posted this question in the inappropriate community.  

---

