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

Viewed 30357

Why this is happening? I don't really understand:

>>> P = [ [()]*3 ]*3
>>> P
[[(), (), ()], [(), (), ()], [(), (), ()]]
>>> P[0][0]=1
>>> P
[[1, (), ()], [1, (), ()], [1, (), ()]]
4 Answers
Related