Quirky Python Traps
Posted on September 2, 2022
Tags: codeetc
1 2d array
= [[99999] * 3] * 4
dp2 0][0] = 1
dp2[#you expect it to change one cell but WRONG
1, 99999, 99999],
[[1, 99999, 99999],
[1, 99999, 99999],
[1, 99999, 99999]] [
The list object we actually created is 4 references to the same 3-length list
1 --> listX: [99999,99999,99999]
2 --> listX: [99999,99999,99999]
3 --> listX: [99999,99999,99999]
4 --> listX: [99999,99999,99999]
dp2[0][0]
causes to change listX object which means all 4 references now point to a changed object.