Python - Check for None value in Matrix
Last Updated :
15 Feb, 2023
Python supports a list as its list element and hence a matrix can be formed. Sometimes we might have a utility in which we require to perform None check in that list of list i.e matrix and its a very common in all the domains of coding, especially Data Science. Let’s discuss certain ways in which this can be performed.
Method #1: Using any() + list comprehension The any function can be used to perform the task of if condition and the check for each element in the nested list can be computed using the list comprehension.
Python3
# Python3 code to demonstrate
# Search in Matrix
# using any() + list comprehension
# initializing list
test_list = [[4, 5, 6],
[10, 2, None],
[1, 11, 18]]
# printing original list
print("The original list : " + str(test_list))
# using any() + list comprehension
# to Search in Matrix
res = any(None in sub for sub in test_list)
# printing result
print("Does Matrix contain None value ? : " + str(res))
OutputThe original list : [[4, 5, 6], [10, 2, None], [1, 11, 18]]
Does Matrix contain None value ? : True
Time Complexity: O(n*m) where n is the number of rows in the matrix and m is the number of columns in the matrix
Auxiliary Space: O(1)
Method #2 : Using set.issubset() + itertools.chain() The issubset method can be used to check for the membership in sublist and chain function can be used to perform this task for the each element in the Matrix, in a faster way as it works on iterators.
Python3
# Python3 code to demonstrate
# Check for None value in Matrix
# using set.issubset() + itertools.chain()
from itertools import chain
# initializing list
test_list = [[4, 5, 6],
[10, 2, None],
[1, 11, 18]]
# printing original list
print("The original list : " + str(test_list))
# using set.issubset() + itertools.chain()
# to Search in Matrix
res = {None}.issubset(chain.from_iterable(test_list))
# printing result
print("Does Matrix contain None value ? : " + str(res))
OutputThe original list : [[4, 5, 6], [10, 2, None], [1, 11, 18]]
Does Matrix contain None value ? : True
Time Complexity: O(n*m) where n is the number of rows in the matrix and m is the number of columns in the matrix
Auxiliary Space: O(1)
Method #3 : Using extend() method and in operator
Python3
# Python3 code to demonstrate
# Search None in Matrix
# initializing list
test_list = [[4, 5, 6],
[10, 2, None],
[1, 11, 18]]
# printing original list
print("The original list : " + str(test_list))
res=False
x=[]
for i in test_list:
x.extend(i)
if None in x:
res=True
# printing result
print("Does Matrix contain None value ? : " + str(res))
OutputThe original list : [[4, 5, 6], [10, 2, None], [1, 11, 18]]
Does Matrix contain None value ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using filter()+lambda functions
Python3
# Python3 code to demonstrate
# Search in Matrix
# initializing list
test_list = [[4, 5, 6],
[10, 2, None],
[1, 11, 18]]
# printing original list
print("The original list : " + str(test_list))
res = []
for i in test_list:
res.extend(i)
res = list(filter(lambda x: x == None, res))
if(res):
res = True
else:
res = False
# printing result
print("Does Matrix contain None value ? : " + str(res))
OutputThe original list : [[4, 5, 6], [10, 2, None], [1, 11, 18]]
Does Matrix contain None value ? : True
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #5 : Using extend() and count() methods
Python3
# Python3 code to demonstrate
# Search None in Matrix
# initializing list
test_list = [[4, 5, 6],
[10, 2, None],
[1, 11, 18]]
# printing original list
print("The original list : " + str(test_list))
res=False
x=[]
for i in test_list:
x.extend(i)
if x.count(None)>=1:
res=True
# printing result
print("Does Matrix contain None value ? : " + str(res))
OutputThe original list : [[4, 5, 6], [10, 2, None], [1, 11, 18]]
Does Matrix contain None value ? : True
Time Complexity : O(N)
Auxiliary Space : O(N)
Method#6: Using itertools.chain() and any()
Python3
import itertools
test_list = [[4, 5, 6],
[10, 2, None],
[1, 11, 18]]
# printing original list
print("The original list : " + str(test_list))
res = any(val == None for val in itertools.chain(*test_list))
print("Does Matrix contain None value ? : " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original list : [[4, 5, 6], [10, 2, None], [1, 11, 18]]
Does Matrix contain None value ? : True
Method#7: Convert to string and check for None
Time Complexity : O(N)
Auxiliary Space : O(1)
Python3
# Python3 code to demonstrate
# Search None in Matrix
# initializing list
test_list = [[4, 5, 6],
[10, 2, None],
[1, 11, 18]]
# printing original list
print("The original list : " + str(test_list))
res = "None" in str(test_list)
# printing result
print("Does Matrix contain None value ? : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [[4, 5, 6], [10, 2, None], [1, 11, 18]]
Does Matrix contain None value ? : True
Time Complexity : O(N)
Auxiliary Space : O(1)
Similar Reads
Python | Check for None Tuple Sometimes, while working with Python records, we can have a problem in which we need to filter out all the tuples which contain just None values. This can have a possible application in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using all() + gen
6 min read
Python | Check for None values in given dictionary Many times, while working with dictionaries, we wish to check for a non-null dictionary, i.e check for None values in given dictionary. This finds application in Machine Learning in which we have to feed data with no none values. Let's discuss certain ways in which this task can be performed. Method
7 min read
Python - Flag None Element Rows in Matrix Given a Matrix, return True for rows which contain a None Value, else return False. Input : test_list = [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8, None]] Output : [True, True, False, True] Explanation : 1, 2 and 4th index contain None occurrence. Input : test_list = [[2, 4, None, 3], [3
4 min read
How to check NoneType in Python Checking NoneType in Python means determining whether a variable holds the special None value, which represents the absence of a value or a null reference. For example, a variable returned from a function might be None if no result is found. It's important to identify NoneType correctly, especially
3 min read
Python | Check if tuple has any None value Sometimes, while working with Python, we can have a problem in which we have a record and we need to check if it contains all valid values i.e has any None value. This kind of problem is common in data preprocessing steps. Let's discuss certain ways in which this task can be performed. Method #1 : U
5 min read
Python Program to check if matrix is upper triangular Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}}; Output : Matrix is in
2 min read