Why does list index reassignment of a copied-two-dimensional list in python affects the original list?

I tried this

ori = [[1,2,3],[4,5,6]]
temp = ori.copy()
temp[0][0] = 200

Result of print ori

[[200, 2, 3], [4, 5, 6]]

But assisgning a value one-dimensionally leaves the ori intact, like so

temp[0] = 200

Ori remains

[[1,2,3],[4,5,6]]

But temp becomes

[200,[4,5,6]]

The above does not seem to change the ori.

Please why is it so and how do I go about modifying a copied list two-dimensionally in python?

I even tried .append() on temp two-dimensionally and it still affected ori.

Thanks.

seems .copy() makes a shallow copy by default:

https://docs.python.org/3.8/library/copy.html

seems you want deepCopy

3 Likes

Yeah, thanks. I have been able to get around it with deepcopy.