Emanuel Barry added the comment:
You do raise a valid point, sequence repetition (*) on lists of mutable objects
serves very little purpose. However, as I stated, it is a side-effect of the
data model and changing it would be yet another special case, which aren't
special enough to break the
Matthew Tanous added the comment:
It makes sense, except for this case is not true when "x" is an immutable data
type - it appears as though something like [5] * n creates a list of totally
separate elements (even if they start as the same thing).
It is difficult to see a case where I would
Emanuel Barry added the comment:
The reason why Python behaves like that in this case is a side-effect of the
way the data model is implemented. Lists are a mutable data type, which means
that referencing the same list - which is what you are doing - as opposed to
creating a new one each
Matthew Tanous added the comment:
I'm aware that it's how Python works at present. My point was that this is
awkward and counter-intuitive, with no purpose I can see worth serving.
"That's just how it works" seems to me a rather insufficient answer, especially
for such a seemingly trivial
Benjamin Peterson added the comment:
That's just how Python works.
https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list
--
nosy: +benjamin.peterson
resolution: -> not a bug
status: open -> closed
___
Python tracker
New submission from Matthew Tanous:
If I produce a list in this fashion:
l = [[x] * n] * n
I would expect that I would obtain a matrix-like structure. Instead, I get a
list of the *same* list, such that the statement:
l[x][y] = z
would change, in essence, every value in "column" y. This