This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Why am I getting inconsistent results in this simple List assignment?
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ammar2, pysolo
Priority: normal Keywords:

Created on 2019-09-21 17:06 by pysolo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg352945 - (view) Author: Srinivasan Samuel (pysolo) Date: 2019-09-21 17:06
Here is the my direct cut & paste from my Python 3.7.3 Shell
>>> C = 2*[[]]
>>> C
[[], []]
>>> 
>>> M = [[],[]]
>>> M
[[], []]
>>> 
>>> C == M
True
>>> 
>>> M[0].append(5)
>>> M
[[5], []]
>>> 
>>> C[0].append(5)
>>> C
[[5], [5]]
>>> 
>>> C == M
False
>>>
msg352946 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2019-09-21 17:20
Check out this part of the FAQ: https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list

Essentially, when you did `C = 2*[[]]`, what happens is that the SAME empty list is placed into C[0] and C[1]. Whereas when you do `M = [[],[]]`, you're creating two different lists. You can confirm this using:

>>> C = 2*[[]]
>>> C[0] is C[1]
True
>>> M = [[],[]]
>>> M[0] is M[1]
False
msg352961 - (view) Author: Srinivasan Samuel (pysolo) Date: 2019-09-22 05:54
Thanks Ammar for your time, service and reply. It was really helpful. I learned some thing more.God Bless you.Srinivasan Samuel
    On Saturday, September 21, 2019, 10:50:32 PM GMT+5:30, Ammar Askar <report@bugs.python.org> wrote:  

Ammar Askar <ammar@ammaraskar.com> added the comment:

Check out this part of the FAQ: https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list

Essentially, when you did `C = 2*[[]]`, what happens is that the SAME empty list is placed into C[0] and C[1]. Whereas when you do `M = [[],[]]`, you're creating two different lists. You can confirm this using:

>>> C = 2*[[]]
>>> C[0] is C[1]
True
>>> M = [[],[]]
>>> M[0] is M[1]
False

----------
nosy: +ammar2
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue38245>
_______________________________________
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82426
2019-09-22 05:54:27pysolosetmessages: + msg352961
2019-09-21 17:20:26ammar2setstatus: open -> closed

nosy: + ammar2
messages: + msg352946

resolution: not a bug
stage: resolved
2019-09-21 17:06:30pysolocreate