Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Class

sparse.lil_matrix

scipy.sparse.lil_matrix is a row-based sparse matrix class suited to incremental construction. It accepts dense or sparse input, or a shape for creating an empty matrix.

How it works

lil_matrix stores a two-dimensional sparse matrix in row-oriented lists, making incremental construction convenient. It can be initialized from dense or sparse data, or from a shape; after construction, convert it to another sparse format when arithmetic or computation requires a different representation.

Syntax

scipy.sparse.lil_matrix(arg1, shape=None, dtype=None, copy=False, *, maxprint=None)

Parameters

NameTypeDescription
arg1array_like, sparse array/matrix, or tupleInput data, another sparse object, or a shape specification such as (M, N).
shape2-tuple of int, optionalMatrix shape when supplied separately.
dtypedtype, optionalData type of stored values; if omitted, the documented default is dtype='d'.
copybool, optionalWhether to copy input data when constructing from an existing object.
maxprintint, optionalMaximum number of items printed in a representation.

Returns

A scipy.sparse.lil_matrix instance representing the supplied data or requested shape.

Key points

  • The class uses a row-oriented list-of-lists representation for sparse matrices.
  • Its main role is building sparse matrices through incremental updates.
  • A two-dimensional NumPy array can be used as the input data.
  • An existing sparse array or matrix can be converted into LIL form.
  • A shape tuple creates an empty matrix, with dtype optionally supplied.
  • When omitted, the documented default data type is double precision.
  • Each row stores column indices in sorted lists, alongside corresponding values.
  • Values corresponding to row indices are stored in the parallel data structure.

Common mistakes

  • Individual insertion may be slow in the worst case; pre-sort column indices within each row when adding many entries.
  • nnz counts stored entries even when their values are explicitly zero.

Example: Build a sparse matrix incrementally

from scipy.sparse import lil_matrix

A = lil_matrix((3, 4), dtype=float)
A[0, 1] = 2.5
A[2, 3] = 7

print(A.toarray())
print(A.nnz)

Creates an empty 3-by-4 LIL matrix, assigns two entries, and converts it to a dense array for display.

When to use it

Incremental assembly

Build a sparse matrix by assigning entries progressively, especially when the final nonzero pattern is not known up front.

Representation conversion

Convert a dense two-dimensional array or another sparse representation into row-oriented LIL storage.

Preallocated sparse structure

Create a correctly shaped sparse matrix before populating selected row and column positions.

Interview questions

QWhat is lil_matrix designed for?

lil_matrix is a row-based list-of-lists sparse matrix intended for incremental construction. It can be initialized from a two-dimensional dense array, another sparse matrix, or a shape tuple.

Follow-up: Which insertion strategy improves construction speed? Pre-sort inserted indices by row and column so additions avoid unnecessary list-search work.

QHow do you construct an empty LIL matrix?

Use lil_matrix((M, N), dtype=...) to create an empty matrix with the requested dimensions and data type. Assign entries by row and column, then use a suitable sparse conversion for later computation.

Follow-up: Why not use LIL for every sparse operation? Its row-list representation is construction-oriented; another sparse format may be more appropriate for computation.

QWhat does nnz mean for a lil_matrix?

The nnz attribute reports the number of stored values, and the documentation explicitly includes stored zeros in that count. Therefore nnz is not necessarily the count of mathematically nonzero values.

Follow-up: How would you inspect the actual dense values? Convert the matrix with toarray(), while remembering that dense conversion can require substantially more memory.

  • scipy.sparse.csr_matrix
  • scipy.sparse.csc_matrix
  • scipy.sparse.coo_matrix
  • scipy.sparse.dok_matrix
  • scipy.sparse.issparse

Source: scipylil_matrix — SciPy v1.18.0 Manual. Checked 2026-10-15.