I'm a beginner in programming, and I'm trying to write code for transposing a matrix. When I run the below code, a logic error occurs:
def transpose(matrix):
x=0
trans=len(matrix[0])*[[]]
for list in matrix:
for element in list:
trans[x].append(element)
x+=1
x=0
return trans
transpose([[1,2,3], [4,5,6]])
Output: [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]
But when I replace line 3 with trans=[[ ],[ ],[ ]]
the code works good! What's the difference between [[ ],[ ],[ ]]
and 3*[[ ]]
? Thanks