Week 5: Python: Exercise Problem on Lists

Can I use the below code? It seems to work.

A = [
    [1,1],
    [1,0]
]

def square_matrix_power(A,n) :
    result = [item[:] for item in A]
    for i in range(n-1):
        temp = [item[:] for item in result]
        result[0][0] = temp[0][0] * A[0][0] + temp[0][1] * A[1][0]
        result[0][1] = temp[0][0] * A[0][1] + temp[0][1] * A[1][1]
        result[1][0] = temp[1][0] * A[0][0] + temp[1][1] * A[1][0]
        result[1][1] = temp[1][0] * A[0][1] + temp[1][1] * A[1][1]
        
    return result

The only difference being

temp = [item[:] for item in result]

at the beginning instead of the end. Is there a difference?

I think not a issue , as long as it is in the loop

1 Like