list in list | SoloLearn: Learn to code for FREE!


list in list

I am expecting only grid[2][2] is altered,but why is every 3rd value in the sublists altered? Is there any way to solve it?

8/21/2019 3:10:40 AM

Hoh Shen Yien

1 Answer

New Answer


By plainly multiplying lists, what you are doing is creating multiple objects that point to the same address. Therefore a change to one is a change to all. What you can do is: [[[] for I in range(3)] for J in range(3)]