How to check if a tuple has any None value in Python

In this blog, we are going to see how we can check a tuple for None value in Python.
We will provide a sample tuple with a None value and make sure the code checks the tuple and gives a positive output stating that None value is indeed present.
Such codes help us filter data when placed in tuples.
These filtering tools come to a lot of handy when we are dealing with a large amount of data as it is in the case of Data Science and Analytics.
Here we will be using the “not all” function to check for the value we desire.

Python program to check if a tuple has any None value

# Checking Tuple for none value

# sample tuple
sample_tuple_test = (11, 14, 75, 246, None)

# printing orignally provided tuple
print("The sample tuple provided is : " + str(sample_tuple_test))

# Checking for None value in the provoded tuple

bool_result = not all(sample_tuple_test)

print("Sample tuple contains a None value? : " + str(bool_result))

Output

check if a tuple has any None value in Python

Background

The “Not All” function is nothing but a compound function created by placing a “Not” keyword in front of the “All” function.
The “All” function returns true value to the receiving variable if all the values are true or empty.
It is boolean in nature and has the following values:-

 

All values are true – True
All values are false – False
One value is true and others are false – False
One value is false and others are true – True
Empty Iterable – True

It is also used to stop the execution of code once the result is generated.

Also read: How to create a tuple of random elements in Python

Leave a Reply