Aug-05-2021, 10:16 PM
(This post was last modified: Aug-05-2021, 10:20 PM by shadowphile.)
Running 3.9.x in Jupyter Notebook
I used a shortcut to create 3x3 array, why doesn't it act correctly?
I used a shortcut to create 3x3 array, why doesn't it act correctly?
brd=[[' ']*3]*3 b=[[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']] print(b==brd) #if these are equivalent, why doesn't the *3 method update correctly? brd[0][0]='X' b[0][0]='X' print(brd) print(b)For those not running the code, line 3 prints True but the result of updating a single entry is not correct using the *3 method, here are the outputs:
Output:[['X', ' ', ' '], ['X', ' ', ' '], ['X', ' ', ' ']]
[['X', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
thanks