Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[[' ']*3]*3 buggy behavior?
#1
Running 3.9.x in Jupyter Notebook
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
Reply


Messages In This Thread
[[' ']*3]*3 buggy behavior? - by shadowphile - Aug-05-2021, 10:16 PM
RE: [[' ']*3]*3 buggy behavior? - by deanhystad - Aug-06-2021, 03:22 AM
RE: [[' ']*3]*3 buggy behavior? - by shadowphile - Aug-06-2021, 07:40 PM
RE: [[' ']*3]*3 buggy behavior? - by naughtyCat - Aug-18-2021, 03:35 PM
RE: [[' ']*3]*3 buggy behavior? - by shadowphile - Aug-18-2021, 07:26 PM
RE: [[' ']*3]*3 buggy behavior? - by deanhystad - Sep-07-2021, 04:26 PM
RE: [[' ']*3]*3 buggy behavior? - by shadowphile - Sep-07-2021, 05:28 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020