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

+3

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? https://code.sololearn.com/cD3HgLUkQ78g/?ref=app

8/21/2019 3:10:40 AM

Hoh Shen Yien

1 Answer

New Answer

+7

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)]