Python Check for None value in Matrix (original) (raw)

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

test_list = [[ 4 , 5 , 6 ],

`` [ 10 , 2 , None ],

`` [ 1 , 11 , 18 ]]

print ( "The original list : " + str (test_list))

res = any ( None in sub for sub in test_list)

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

from itertools import chain

test_list = [[ 4 , 5 , 6 ],

`` [ 10 , 2 , None ],

`` [ 1 , 11 , 18 ]]

print ( "The original list : " + str (test_list))

res = { None }.issubset(chain.from_iterable(test_list))

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

test_list = [[ 4 , 5 , 6 ],

`` [ 10 , 2 , None ],

`` [ 1 , 11 , 18 ]]

print ( "The original list : " + str (test_list))

res = False

x = []

for i in test_list:

`` x.extend(i)

if None in x:

`` res = True

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

test_list = [[ 4 , 5 , 6 ],

`` [ 10 , 2 , None ],

`` [ 1 , 11 , 18 ]]

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

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

test_list = [[ 4 , 5 , 6 ],

`` [ 10 , 2 , None ],

`` [ 1 , 11 , 18 ]]

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

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

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

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

test_list = [[ 4 , 5 , 6 ],

`` [ 10 , 2 , None ],

`` [ 1 , 11 , 18 ]]

print ( "The original list : " + str (test_list))

res = "None" in str (test_list)

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