Is this a bug in Python or something I do not understand.

Miles semanticist at gmail.com
Thu Jan 1 19:20:51 CET 2009


On Thu, Jan 1, 2009 at 1:13 PM,  <davidalvi at gmail.com> wrote:
> Consider these two lists comprehensions:
>
> L1=[[1 for j in range(3)] for i in range(3)]
> L2=[[1]*3]*3
>
[snip]
>
> It seems a misbehaviour in Python, or there is something I do not
> understand in the syntax ????

It's not a Python bug.  Does this help illuminate the difference?

>>> L1 = [object() for j in range(3)]
>>> L2 = [object()] * 3
>>> [id(o) for o in L1]
[164968, 164976, 164984]
>>> L1[0] is L1[1]
False
>>> [id(o) for o in L2]
[164992, 164992, 164992]
>>> L2[0] is L2[1]
True

-Miles



More information about the Python-list mailing list