Python program to Convert a Matrix to Sparse Matrix Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indices as keys and the corresponding values as dictionary values. Python m = [ [1, 0, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4] ] sparse_matrix = {} for i in range(len(m)): for j in range(len(m[i])): if m[i][j] != 0: sparse_matrix[(i, j)] = m[i][j] print(sparse_matrix) Output{(0, 0): 1, (1, 2): 3, (2, 3): 4} Explanation:The program iterates through each element of the matrix and stores non-zero elements in a dictionary, where the key is a tuple of indices (i, j) and the value is the element.This reduces memory usage by only storing non-zero values.Using a List of TuplesConverting a matrix to a sparse matrix using a list of tuples involves storing the non-zero elements as tuples where each tuple contains the row index, column index and the value. This method is efficient for representing sparse matrices while maintaining the order of the elements. Python m = [ [1, 0, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4] ] sparse_matrix = [] for i in range(len(m)): for j in range(len(m[i])): if m[i][j] != 0: sparse_matrix.append((i, j, m[i][j])) print(sparse_matrix) Output[(0, 0, 1), (1, 2, 3), (2, 3, 4)] Explanation:The program creates a list of tuples, each containing the row index, column index, and non-zero value of the matrix.This format allows easy tracking of positions and values for sparse matrices. Python program to Convert a Matrix to Sparse Matrix Comment More infoAdvertise with us Next Article How to Create a Sparse Matrix with SciPy R riturajsaha Follow Improve Article Tags : Python Python Programs python-list Python list-programs Python matrix-program +1 More Practice Tags : pythonpython-list Similar Reads Python Program to Check if a given matrix is sparse or not A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem 4 min read Python Program to Construct n*m Matrix from List We are given a list we need to construct a n*m matrix from that list. For example, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we need to construct a 3*4 matrix so that resultant output becomes [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] .Using List ComprehensionThis method uses list comprehension 3 min read How to Create a Sparse Matrix with SciPy If most of the elements of the matrix have 0 value, then it is called a sparse matrix. The two major benefits of using sparse matrix instead of a simple matrix are:Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.Computing time: C 4 min read How to Create a Sparse Matrix with SciPy If most of the elements of the matrix have 0 value, then it is called a sparse matrix. The two major benefits of using sparse matrix instead of a simple matrix are:Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.Computing time: C 4 min read Python | Convert LaTeX Matrices into SymPy Matrices using Python A matrix is a rectangular two-dimensional array of data like numbers, characters, symbols, etc. which we can store in row and column format. We can perform operations on elements by using an index (i,j) where 'i' and 'j' stand for an index of row and column respectively. In the Python programming la 5 min read Python - Matrix creation of n*n Matrices are fundamental structures in programming and are widely used in various domains including mathematics, machine learning, image processing, and simulations. Creating an nÃn matrix efficiently is an important step for these applications. This article will explore multiple ways to create such 3 min read Like