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() + generator expression
The combination of above functionalities can be used to perform this particular task. In this we feed the logic of finding None using generator expression and checking for each element is handled by all().
Python3
test_tup = ( None , None , None , None , None )
print ( "The original tuple : " + str (test_tup))
res = all (ele is None for ele in test_tup)
print ( "Does tuple contain all None elements ? : " + str (res))
|
Output :
The original tuple : (None, None, None, None, None)
Does tuple contain all None elements ? : True
Method #2 : Using len() + count() The combination of above functions can be used to perform this task. In this, we just count the occurrences of None and equate to list length to check if all elements are None.
Python3
test_tup = ( None , None , None , None , None )
print ( "The original tuple : " + str (test_tup))
res = len (test_tup) = = test_tup.count( None )
print ( "Does tuple contain all None elements ? : " + str (res))
|
Output :
The original tuple : (None, None, None, None, None)
Does tuple contain all None elements ? : True
Method #3: Using for loop
Python3
test_tup = ( None , None , None , None , None )
print ( "The original tuple : " + str (test_tup))
res = False
c = 0
for i in test_tup:
if i is None :
c + = 1
if (c = = len (test_tup)):
res = True
print ( "Does tuple contain all None elements ? : " + str (res))
|
Output
The original tuple : (None, None, None, None, None)
Does tuple contain all None elements ? : True
Method #4 : Using len() and tuple() methods
Python3
test_tup = ( None , None , None , None , None )
print ( "The original tuple : " + str (test_tup))
res = False
x = [ None ] * len (test_tup)
x = tuple (x)
if (x = = test_tup):
res = True
print ( "Does tuple contain all None elements ? : " + str (res))
|
Output
The original tuple : (None, None, None, None, None)
Does tuple contain all None elements ? : True
Method #5: Using filter()+list()+ lambda functions
Python3
test_tup = ( None , None , None , None , None )
print ( "The original tuple : " + str (test_tup))
res = not list ( filter ( lambda x: x ! = None , test_tup))
print ( "Does tuple contain all None elements ? : " + str (res))
|
Output
The original tuple : (None, None, None, None, None)
Does tuple contain all None elements ? : True
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #6: Using recursive method.
Python3
def is_None(start,lst):
if start = = len (lst):
return True
if lst[start] ! = None :
return False
return is_None(start + 1 ,lst)
test_tup = ( None , None , None , None , None )
print ( "The original tuple : " + str (test_tup))
res = is_None( 0 ,test_tup)
print ( "Does tuple contain all None elements ? : " + str (res))
|
Output
The original tuple : (None, None, None, None, None)
Does tuple contain all None elements ? : True
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #7 : Using len() + operator.countOf()
Approach
- Initialize a tuple
- Check whether length of tuple is equal to count of None’s in the tuple(operator.countOf())
- Assign the boolean value to res
- Display the result variable
Python3
test_tup = ( None , None , None , None , None )
print ( "The original tuple : " + str (test_tup))
import operator
res = len (test_tup) = = operator.countOf(test_tup, None )
print ( "Does tuple contain all None elements ? : " + str (res))
|
Output
The original tuple : (None, None, None, None, None)
Does tuple contain all None elements ? : True
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #8 : Using not any() function:
Algorithm:
1.Create a tuple test_tup with 5 elements, all set to None.
2.Use a generator expression to iterate over each element in the tuple and check if it is not None.
3.The any() function is used to determine if any of the elements returned by the generator expression are True.
4.If any element is not None, the generator expression returns True, and the any() function returns True.
5.The not operator is used to invert the boolean value returned by any(). This gives us True if all elements in the tuple are None, and False otherwise.
6.The final boolean result is stored in the variable res.
7.The print() function is used to output the value of res.
Python3
test_tup = ( None , None , None , None , None )
res = not any (elem is not None for elem in test_tup)
print (( "Does tuple contain all None elements ? : " + str (res)))
|
Output
Does tuple contain all None elements ? : True
Time complexity :O(N), where N is the number of elements in the tuple test_tup.
The auxiliary space : O(1), because it uses a fixed amount of memory to store the tuple and the boolean result res.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Mar, 2023
Like Article
Save Article