I've written the following code below but the output of it is not as I expected. Does anyone know why it is behaving like this?
N.B.: I know the code doesn't transpose the list correctly - I stumbled across this strange behavior whilst writing the function.
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
transposed = []
for i in range(4):
print("i", i)
t_list = []
for row in matrix:
print("row", row)
t_list.append(row[i])
print("t_list**********", t_list)
transposed.append(t_list)
print("transposed//////////////", transposed)
The output I would expect from this function at the end of the first row is:
[[1], [1, 5], [1, 5, 9]]
Instead, it seems to output:
[[1, 5, 9], [1, 5, 9], [1, 5, 9]]
Does anyone know why?
Thanks!