list in list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

21st Aug 2019, 3:10 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
1 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)]
21st Aug 2019, 3:29 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar