L1 is a list of three different lists, although each list holds the
same values.
L2 is a list of three references to the same list (the '*' operator
doesn't do a deep copy). So when you modify any of the referenced
lists, you modify all of them.
Try this:
>>> q = [1, 1, 1]
>>> r = [q, q, q]
>>>
On Thu, Jan 1, 2009 at 10:13 AM, wrote:
> Consider these two lists comprehensions:
>
> L1=[[1 for j in range(3)] for i in range(3)]
> L2=[[1]*3]*3
> So far, everything is OK, but let us now modify the lists' contents in
> the following way:
> It seems a misbehaviour in Python, or there is somet
On Thu, Jan 1, 2009 at 1:13 PM, 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
Consider these two lists comprehensions:
L1=[[1 for j in range(3)] for i in range(3)]
L2=[[1]*3]*3
print L1
print L2
print L1==L2
The result is:
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
True
So far, everything is OK, but let us now modify the lists' contents in
the f