Python list multiplication: [[…]]*3 makes 3 lists which mirror each other when modified [duplicate]

You’ve made 3 references to the same list.

>>> a = b = []
>>> a.append(42)
>>> b
[42]

You want to do this:

P = [[()] * 3 for x in range(3)]

Leave a Comment