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 : Using any() + map() + lambda Combination of above functions can be used to perform this task. In this, we check for any element using any(), and extension of logic is done by map() and lambda.
Python3
test_tup = ( 10 , 4 , 5 , 6 , None )
print ( "The original tuple : " + str (test_tup))
res = any ( map ( lambda ele: ele is None , test_tup))
print ( "Does tuple contain any None value ? : " + str (res))
|
Output : The original tuple : (10, 4, 5, 6, None)
Does tuple contain any None value ? : True
Method #2: Using not + all() This checks for the truthness of all elements of the tuple using all() and with not, returns True if there is no None element.
Python3
test_tup = ( 10 , 4 , 5 , 6 , None )
print ( "The original tuple : " + str (test_tup))
res = not all (test_tup)
print ( "Does tuple contain any None value ? : " + str (res))
|
Output : The original tuple : (10, 4, 5, 6, None)
Does tuple contain any None value ? : True
Method #3: Using in operator
Python3
test_tup = ( 10 , 4 , 5 , 6 , None )
print ( "The original tuple : " + str (test_tup))
res = None in test_tup
print ( "Does tuple contain any None value ? : " + str (res))
|
OutputThe original tuple : (10, 4, 5, 6, None)
Does tuple contain any None value ? : True
Method #4 : Using count() method
Python3
test_tup = ( 10 , 4 , 5 , 6 , None )
print ( "The original tuple : " + str (test_tup))
res = False
if (test_tup.count( None ) > = 1 ):
res = True
print ( "Does tuple contain any None value ? : " + str (res))
|
OutputThe original tuple : (10, 4, 5, 6, None)
Does tuple contain any None value ? : True
Method #5 : Using filter()+list()+len()+ lambda functions
Python3
test_tup = ( 10 , 4 , 5 , 6 , None )
print ( "The original tuple : " + str (test_tup))
res = len ( list ( filter ( lambda x: x = = None , test_tup))) > 0
print ( "Does tuple contain any None value ? : " + str (res))
|
OutputThe original tuple : (10, 4, 5, 6, None)
Does tuple contain any None value ? : True
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #6: Using itertools.filterfalse()
Python3
import itertools
import itertools
test_tup = ( 10 , 4 , 5 , 6 , None )
print ( "The original tuple : " + str (test_tup))
res = len ( list (itertools.filterfalse( lambda x: x is None , test_tup))) < len (test_tup)
print ( "Does tuple contain any None value ? : " + str (res))
|
OutputThe original tuple : (10, 4, 5, 6, None)
Does tuple contain any None value ? : True
a time complexity of O(n) where n is the number of elements in the tuple. The space complexity is also O(n) for the method which creates a new list or set from the tuple.
Method#7: Using recursion
Python3
def CheckNone(test_tup,i):
if len (test_tup) = = i:
return False
if test_tup[i] = = None :
return True
return CheckNone(test_tup,i + 1 )
test_tup = ( 10 , 4 , 5 , 6 , None )
print ( "The original tuple : " + str (test_tup))
res = CheckNone(test_tup, 0 )
print ( "Does tuple contain any None value ? : " + str (res))
|
OutputThe original tuple : (10, 4, 5, 6, None)
Does tuple contain any None value ? : True
Time Complexity: O(N)
Auxiliary Space: O(N)
Method#7: Using for loop and is operator.
Python3
test_tup = ( 10 , 4 , 5 , 6 , None )
flag = False
print ( "The original tuple : " + str (test_tup))
for i in test_tup:
if i is None :
flag = True
break
print ( "Does tuple contain any None value ? : " + str (flag))
|
OutputThe original tuple : (10, 4, 5, 6, None)
Does tuple contain any None value ? : True
Time Complexity: O(N)
Auxiliary Space: O(1)