[issue38245] Why am I getting inconsistent results in this simple List assignment?

Srinivasan Samuel report at bugs.python.org
Sun Sep 22 01:54:27 EDT 2019


Srinivasan Samuel <creatorsss at yahoo.com> added the comment:

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 at bugs.python.org> wrote:  

Ammar Askar <ammar at 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 at bugs.python.org>
<https://bugs.python.org/issue38245>
_______________________________________

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38245>
_______________________________________


More information about the Python-bugs-list mailing list