Check for None value in a Matrix in Python
Hello Friends, In this tutorial, we will look at how to check if any of the value is None in the given matrix and print the position of None value in Python. None is used for the null value in Python.
Check if None is there or not in a Matrix
First, we take a matrix named as mat of size 3 by 3 where we have some values as None.
mat = [
As we clearly see the matrix mat does have None values at (1,1) and (2,0) considering 0-based indexing.
The idea is to iterate our matrix line by line and check if None is present or not in the line. If None value is present in this line we print its position.
Below is the implementation of the above approach in Python coding:
mat = [ [1,2,None], [4,None,6], [None,8,9] ] for line in mat: if(None in line): print(True) break; i=-1 for line in mat: i+=1 j=-1 for val in line: j+=1 if(val == None): print(i,j)
True 0 2 1 1 2 0
The explanation for the output:
mat = [
As we can see, input in the code and we see there are None present in the given matrix at the position of (0,2), (1,1) and (2,0). Hence the output of our code is True followed by the position of None value considering 0-based indexing for rows and columns.