creating a scipy.lil_matrix using a python generator efficiently
I have a generator that generates single dimension numpy.array s of the same length. I would like to have a sparse matrix containing that data. Rows are generated in the same order I'd like to have them in the final matrix. csr matrix is preferable over lil matrix, but I assume the latter will be easier to build in the scenario I'm describing. Assuming row_gen is a generator yielding numpy.array rows, the following code works as expected. def row_gen(): yield numpy.array([1, 2, 3]) yield numpy.array([1, 0, 1]) yield numpy.array([1, 0, 0]) matrix = scipy.sparse.lil_matrix(list(row_gen()))