Sparse Matrix in Python using Dictionary Last Updated : 07 Jul, 2021 Comments Improve Suggest changes Like Article Like Report A sparse matrix is a matrix in which most of the elements have zero value and thus efficient ways of storing such matrices are required. Sparse matrices are generally utilized in applied machine learning such as in data containing data-encodings that map categories to count and also in entire subfields of machine learning such as natural language processing (NLP). Examples: 0 0 0 1 0 2 0 0 0 3 0 0 0 4 0 Above is sparse matrix with only 4 non-zero elements. Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. These efficient ways require only the non-zero values to be stored along with their index so that the original matrix can be retrieved when required. One such efficient way in Python is the use of a dictionary. Dictionary in Python stores data in key-value pairs like maps in Java. Dictionary stores data in an unordered manner. Approach:First, we take a sparse matrix and create an empty dictionary.Then we iterate through all the elements of the matrix and check if they are zero or non-zero elements.The non-zero elements are added to the dictionary with their index as the key and their data as the value in the key-value pairs of the dictionary.Finally, we can print the dictionary giving each element with its index. Below is the Implementation. Python3 # creating sparse matrix arr = [[0, 0, 0, 1, 0], [2, 0, 0, 0, 3], [0, 0, 0, 4, 0]] # creating empty dictionary dic = {} # iterating through the matrix for i in range(len(arr)): for j in range(len(arr[i])): if arr[i][j] != 0: # adding non zero elements to # the dictionary dic[i, j] = arr[i][j] print("Position of non-zero elements in the matrix:") print(dic) Output: The following dictionary shows the positional of non-zero elements in the sparse matrix {(0, 3): 1, (1, 0): 2, (1, 4): 3, (2, 3): 4} Comment More infoAdvertise with us Next Article Sparse Matrix in Python using Dictionary M mehulp1612 Follow Improve Article Tags : Technical Scripter Python Python-Matrix Practice Tags : python Similar Reads Python 3.6 Dictionary Implementation using Hash Tables Dictionary in Python is a collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictiona 3 min read Few mistakes when using Python dictionary Usually, A dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. Each key-value pair in a Dictionary is separated by a 'colon', whereas each key is separated by a âcommaâ. Python3 1== my_dict = { 3 min read How to reduce dimensionality on Sparse Matrix in Python? A matrix usually consists of a combination of zeros and non-zeros. When a matrix is comprised mostly of zeros, then such a matrix is called a sparse matrix. A matrix that consists of maximum non-zero numbers, such a matrix is called a dense matrix. Sparse matrix finds its application in high dimensi 3 min read Dictionary with Tuple as Key in Python Dictionaries allow a wide range of key types, including tuples. Tuples, being immutable, are suitable for use as dictionary keys when storing compound data. For example, we may want to map coordinates (x, y) to a specific value or track unique combinations of values. Let's explores multiple ways to 4 min read Top 30 Python Dictionary Interview Questions Python dictionaries are important data structures used to store key-value pairs. In this article, we will explore the Top 30 Python Dictionary interview questions to help you prepare for technical interviews.Table of ContentBasic Python Dictionary Interview QuestionsIntermediate Python Dictionary In 7 min read Like