Python program to Convert a Matrix to Sparse Matrix

17 Jan, 2022
Given a matrix with most of its elements as 0, we need to convert this matrix into a sparse matrix in Python.
Example:
Input: Matrix:1 0 0 00 2 0 00 0 3 00 0 0 45 0 0 0
Output: Sparse Matrix:0 0 11 1 22 2 33 3 44 0 5
Explanation:Here the Matrix is represented using a 2D list and the Sparse Matrix is represented in the form Row Column Value
In the Sparse Matrix the first row is 0 1 1 indicates that the value of the Matrix at row 0 and column 1 is 1.
Approach:
Create an empty list which will represent the sparse matrix list.Iterate through the 2D matrix to find non zero elements.If an element is non zero, create a temporary empty list.Append the row value, column value, and the non zero element itself into the temporary list.Now append the temporary list into the sparse matrix list such that the temporary list acts as a sub-list of the sparse matrix list.After getting all the non zero elements from the matrix, display the sparse matrix.
Create an empty list which will represent the sparse matrix list.
Iterate through the 2D matrix to find non zero elements.
If an element is non zero, create a temporary empty list.
Append the row value, column value, and the non zero element itself into the temporary list.
Now append the temporary list into the sparse matrix list such that the temporary list acts as a sub-list of the sparse matrix list.
After getting all the non zero elements from the matrix, display the sparse matrix.
The above approach has been used in convertToSparseMatrix() function in the below program:
# Python program to convert a# matrix to sparse matrix    # function display a matrixdef displayMatrix(matrix):          for row in matrix:        for element in row:            print(element, end =" ")        print()  # function to convert the matrix # into a sparse matrixdef convertToSparseMatrix(matrix):      # creating an empty sparse     # matrix list    sparseMatrix =[]      # searching values greater     # than zero    for i in range(len(matrix)):        for j in range(len(matrix[0])):            if matrix[i][j] != 0 :                  # creating a temporary                # sublist                temp = []                  # appending row value, column                 # value and element into the                 # sublist                 temp.append(i)                temp.append(j)                temp.append(matrix[i][j])                  # appending the sublist into                # the sparse matrix list                sparseMatrix.append(temp)      # displaying the sparse matrix    print("\nSparse Matrix: ")     displayMatrix(sparseMatrix)                   # Driver's code# initializing a normal matrixnormalMatrix =[[1, 0, 0, 0],                [0, 2, 0, 0],                [0, 0, 3, 0],                [0, 0, 0, 4],                [5, 0, 0, 0]]   # displaying the matrixdisplayMatrix(normalMatrix)  # converting the matrix to sparse # displayMatrix convertToSparseMatrix(normalMatrix)       
Output:
0 1 0 0 
0 0 2 0 
0 3 0 0 
0 0 5 0 
0 0 0 4 

Sparse Matrix: 
0 0 1 
1 1 2 
2 2 3 
3 3 4 
4 0 5 

simmytarika5
Python list-programs
Python matrix-program
python-list
Python
Python Programs
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.