In a Python list, why are my appended values not the same values I appended?

I have a mat that looks like this:

mat = [ [0,0,0,0], [6,7,8,9] ]

I want a list of mats that looks like this:

[[1, 0, 0, 0], [6, 7, 8, 9]]
[[0, 1, 0, 0], [6, 7, 8, 9]]
[[0, 0, 1, 0], [6, 7, 8, 9]]
[[0, 0, 0, 1], [6, 7, 8, 9]]

Here’s the code I used:

# 2D List with some values
mat = [ [0,0,0,0], [6,7,8,9] ]

# append to listOfMats new a modified mat
# prints what I want listOfMats to look like
print("What listOfMats should look like:")
listOfMats = []
for x in range(4):
    mat[0][x] = 1
    print(mat)
    listOfMats.append(mat.copy())
    mat[0][x] = 0
print()

print("What I get for listOfMats: ")
for i in listOfMats:
    print(i)

Here’s the output:

What listOfMats should look like:
[[1, 0, 0, 0], [6, 7, 8, 9]]
[[0, 1, 0, 0], [6, 7, 8, 9]]
[[0, 0, 1, 0], [6, 7, 8, 9]]
[[0, 0, 0, 1], [6, 7, 8, 9]]

What I get for listOfMats: 
[[0, 0, 0, 0], [6, 7, 8, 9]]
[[0, 0, 0, 0], [6, 7, 8, 9]]
[[0, 0, 0, 0], [6, 7, 8, 9]]
[[0, 0, 0, 0], [6, 7, 8, 9]]

I think the problem is in this line:

mat[0][x] = 1
print(mat)
listOfMats.append(mat.copy())
mat[0][x] = 0

mat[0][x] is changed to 1, then I append a deep copy of that mat into listOfMats. But somehow, that appended deep copy is changed with mat[0][x] = 0.

What am I doing wrong?

Answer

The comment of John is correct. We need a deep copy of the list here.

A way to do that that works with any kind of lists is to use the copy library:

import copy

mat_copy = copy.deepcopy(mat)

Your full code:

import copy

# 2D List with some values
mat = [ [0,0,0,0], [6,7,8,9] ]

# append to listOfMats new a modified mat
# prints what I want listOfMats to look like
print("What listOfMats should look like:")
listOfMats = []
for x in range(4):
    mat[0][x] = 1
    print(mat)
    listOfMats.append(copy.deepcopy(mat))
    mat[0][x] = 0
print()

print("What I get for listOfMats: ")
for i in listOfMats:
    print(i)