Write a function that takes a matrix and a scalar multiplier as input and returns a new matrix containing the scalar product of the scalar and matrix

Write a function called scalar_matrix_multiply that takes a matrix (represented by a list of numerical lists) and a scalar multiplier (a float) as input and returns a new matrix containing the scalar product of the scalar and the matrix. For example,

>>scalar_matrix_multiply([[4, 2], [-5,1]], 3)
[[12, 6], [-15, 3]

My code:

L = 2
def scalar_matrix_multiply(lst, x):
    for i in range(L):
        for j in range(L):
            lst[i][j] = lst[i][j] * x

Answer

You forgot a return statement (i.e. return lst). You can also use a list comprehension to avoid reassignments.

def scalar_matrix_multiply(lst, x):
    return [[c * x for c in row] for row in lst]

print(scalar_matrix_multiply([[4, 2], [-5,1]], 3))

result:

[[12, 6], [-15, 3]]