explanation for matrix multiplication code

Viewed 24

Can anyone explain what's going on in the line that defines 'c' here? I think I understand it but I want to make sure I really know what's going on rather than just memorizing the operation. It creates a matrix filled with 0's, c = ([0 0 0 0], [0 0 0 0], [0 0 0 0]). Why does it create a nested list rather than just a single list?

def mmult(a, b):
    nrowa = len(a)  # get number of rows and columns in a and b
    ncola = len(a[0])
    nrowb = len(b)
    ncolb = len(b[0])
    if ncola != nrowb:   # is multiplication possible?
        return None  # if not, bail out!
    c = [[0] * ncolb for i in range(nrowa)] 
0 Answers
Related