We know that in python you can multiply a list by a number:
[2]*3=[2,2,2]
I used this idea to create a 2d array:
exploded=[["O"]*7]*6
resulting in
[['O', 'O', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', 'O', 'O']]
However, when I try to change an element of this 2d array:
exploded[0][0]=9
instead of only that one element at position (0,0) changes, all elements at position (x,0) changes where x is 0 to len(exploded)-1.
[[9, 'O', 'O', 'O', 'O', 'O', 'O'], [9, 'O', 'O', 'O', 'O', 'O', 'O'], [9, 'O', 'O', 'O', 'O', 'O', 'O'], [9, 'O', 'O', 'O', 'O', 'O', 'O'], [9, 'O', 'O', 'O', 'O', 'O', 'O'], [9, 'O', 'O', 'O', 'O', 'O', 'O']]
If I assign the 2d array to exploded directly, then this issue doesn't occur, so I think it has to be something about multiplying lists by a number. Can someone explain what the error/bug here is and how [2]*3 actually works?