Sunday, December 29, 2024
Google search engine
HomeLanguagesPython | Check for None Tuple

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




# Python3 code to demonstrate working of
# Check for None Tuple
# using all() + generator expression
 
# initialize tuple
test_tup = (None, None, None, None, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check for None Tuple
# using all() + generator expression
res = all(ele is None for ele in test_tup)
 
# printing result
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




# Python3 code to demonstrate working of
# Check for None Tuple
# using len() + count()
 
# initialize tuple
test_tup = (None, None, None, None, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check for None Tuple
# using len() + count()
res = len(test_tup) == test_tup.count(None)
 
# printing result
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




# Python3 code to demonstrate working of
# Check for None Tuple
 
# initialize tuple
test_tup = (None, None, None, None, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check for None Tuple
res=False
c=0
for i in test_tup:
    if i is None:
        c+=1
if(c==len(test_tup)):
    res=True
# printing result
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




# Python3 code to demonstrate working of
# Check for None Tuple
 
# Initialize tuple
test_tup = (None, None, None, None, None)
 
# Printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check for None Tuple
res=False
x=[None]*len(test_tup)
x=tuple(x)
if(x==test_tup):
    res=True
     
# Printing result
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




# Python3 code to demonstrate working of
# Check for None Tuple
 
# Initialize tuple
test_tup = (None, None, None, None, None)
 
# Printing original tuple
print("The original tuple : " + str(test_tup))
 
res = not list(filter(lambda x: x != None, test_tup))
 
# Printing result
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




# Python3 code to demonstrate working of
# Check for None Tuple
 
#defining recursive function
def is_None(start,lst):
  if start==len(lst):  #base condition
    return True
  if lst[start] != None#checking value is None or not
    return False
  return is_None(start+1,lst)
 
# Initialize tuple
test_tup = (None, None, None, None, None)
 
 
# Printing original tuple
print("The original tuple : " + str(test_tup))
 
#calling recursive function
res=is_None(0,test_tup)
 
# Printing result
print("Does tuple contain all None elements ? : " + str(res))
 
#this code contributed by tvsk


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 

  1. Initialize a tuple
  2. Check whether length of tuple is equal to count of None’s in the tuple(operator.countOf())
  3. Assign the boolean value to res
  4. Display the result variable
     

Python3




# Python3 code to demonstrate working of
# Check for None Tuple
 
# initialize tuple
test_tup = (None, None, None, None, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check for None Tuple
import operator
res = len(test_tup) == operator.countOf(test_tup,None)
 
# printing result
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




# Create a tuple with five elements, all set to None
test_tup = (None, None, None, None, None)
 
# Use a generator expression with the any() function to check if there are any non-None elements in the tuple
# The generator expression iterates over each element in the tuple, and checks if it is not None
# If any element is not None, the generator expression returns True
# The any() function returns True if any of the elements returned by the generator expression are True
# We invert the boolean value returned by any() using the not operator
# This gives us True if all elements in the tuple are None, and False otherwise
res = not any(elem is not None for elem in test_tup)
 
# Print the result
print(("Does tuple contain all None elements ? : " + str(res)))
#This code is contributed by Jyothi pinjala.


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.

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