I'm using the following code to input value to a nested list/matrix. But getting the identical row value assigned in the last row.
rows = int(input('Input the rows: '))
cols = int(input('Input the cols: '))
#rows, cols = (5, 5)
arr1 = [[int(0)]*cols]*rows
print(arr1)
for i in range(rows):
for j in range(cols):
arr1[i][j]= int(input('arr1[%d][%d]: '%(i,j)))
print(arr1[i][j])
print(arr1)
Let's
>>> Input the rows: 2
>>> Input the cols: 2
Then,
>>>rows
2
>>>cols
2
Afterwords,
arr1[0][0]: 22
arr1[0][1]: 33
arr1[1][0]: 44
arr1[1][1]: 55
Lastly, the issue found. Last row elements repeat through all the previous rows.
>>>print(arr1)
[[44, 55], [44, 55]]
It is also occurring for higher dimension matrices.
I don't want this to happen. Instead, I wish to get no repetition, like:
>>>print(arr1)
[[22, 33], [44, 55]]