Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Check if tuple has any None value

Python | Check if tuple has any None value

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




# Python3 code to demonstrate working of
# Check if tuple has any None value
# using any() + map() + lambda
 
# initialize tuple
test_tup = (10, 4, 5, 6, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check if tuple has any None value
# using any() + map() + lambda
res = any(map(lambda ele: ele is None, test_tup))
 
# printing result
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




# Python3 code to demonstrate working of
# Check if tuple has any None value
# using not + all()
 
# initialize tuple
test_tup = (10, 4, 5, 6, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check if tuple has any None value
# using not + all()
res = not all(test_tup)
 
# printing result
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




# Python3 code to demonstrate working of
# Check if tuple has any None value
 
# initialize tuple
test_tup = (10, 4, 5, 6, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check if tuple has any None value
res = None in test_tup
 
# printing result
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 #4 : Using count() method

Python3




# Python3 code to demonstrate working of
# Check if tuple has any None value
 
# initialize tuple
test_tup = (10, 4, 5, 6, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check if tuple has any None value
res = False
if(test_tup.count(None) >= 1):
    res = True
 
# printing result
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 #5 : Using filter()+list()+len()+ lambda functions

Python3




# Python3 code to demonstrate working of
# Check if tuple has any None value
 
# initialize tuple
test_tup = (10, 4, 5, 6, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check if tuple has any None value
res = len(list(filter(lambda x: x == None, test_tup))) > 0
 
# printing result
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

Time Complexity: O(N)

Auxiliary Space: O(N)

Method #6: Using itertools.filterfalse()

Python3




#Python3 code to demonstrate working of
#Check if tuple has any None value
import itertools
import itertools
 
#initialize tuple
test_tup = (10, 4, 5, 6, None)
 
#printing original tuple
print("The original tuple : " + str(test_tup))
 
#Check if tuple has any None value
res = len(list(itertools.filterfalse(lambda x: x is None, test_tup))) < len(test_tup)
 
#printing result
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

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




# Python3 code to demonstrate working of
# Check if tuple has any None value
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)
 
# initialize tuple
test_tup = (10, 4, 5, 6, None)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check if tuple has any None value
res = CheckNone(test_tup,0)
 
# printing result
print("Does tuple contain any None value ? : " + str(res))
 
#This code is contributed Vinay Pinjala.


Output

The 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




#Python3 code to demonstrate working of
#Check if tuple has any None value
 
#initialize tuple
test_tup = (10, 4, 5, 6, None)
 
#initializing flag with False by default
flag=False
 
#printing original tuple
print("The original tuple : " + str(test_tup))
 
#Check if tuple has any None value using for loop
for i in test_tup:
  if i is None:
    flag=True
    break
 
 
#printing result
print("Does tuple contain any None value ? : " + str(flag))
 
#This code contributed by tvsk


Output

The original tuple : (10, 4, 5, 6, None)
Does tuple contain any None value ? : True

Time Complexity: O(N)

Auxiliary Space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments