def add(*matrices): get_shape = lambda matrix: [len(vector) for vector in matrix] shape = get_shape(matrices[0]) if any(get_shape(matrix) != shape for matrix in matrices): raise ValueError("Given matrices are not the same size.") return [[sum(elt) for elt in zip(*vectors)] for vectors in zip(*matrices)]