Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Check for None value in Matrix

Python – Check for None value in Matrix

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))


Output

The 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))


Output

The 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))


Output

The 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))


Output

The 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))


Output

The 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.


Output

The 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


Output

The 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)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments