I want to iterate over a 2d python list. However, I have noticed a difference when it iterates over an empty 2d array vs a nonempty 2d array. For example
mat = [[]] * n
print("initial", mat)
count = 0
for row in mat:
print("row:", row)
for _ in range(2):
print(_)
row.append(count)
count += 1
print(mat)
print("\n")
return mat
mat = empty(2)
This is what it prints. It is appending the count to both the sub-arrays instead of just one
initial [[], []]
row: []
0
[[0], [0]]
1
[[0, 1], [0, 1]]
row: [0, 1]
0
[[0, 1, 2], [0, 1, 2]]
1
[[0, 1, 2, 3], [0, 1, 2, 3]]
However when I change the code to this
def nonempty(n):
mat = [['a'],['b']]
print("initial", mat)
count = 0
for row in mat:
print("row:", row)
for _ in range(2):
print(_)
row.append(count)
count += 1
print(mat)
print("\n")
return mat
mat = empty(2)
This is what it prints
initial [['a'], ['b']]
row: ['a']
0
[['a', 0], ['b']]
1
[['a', 0, 1], ['b']]
row: ['b']
0
[['a', 0, 1], ['b', 2]]
1
[['a', 0, 1], ['b', 2, 3]]
In this case, it only changes one sub array at a time instead of two. Can anyone tell me why this is happening. Is it because of how python lists work?
