Q:

Python program to check if the tuple has any none value

belongs to collection: Python Tuple Programs

0

Example:

tuple = ("python", "includehelp", 43, 54.23)

Checking if the tuple has any None Value

In this article, we have a tuple consisting of N values. We need to create a program in Python to check if the tuple contains any None value.

Input:
(4, 8, none, 2)

Output:
true

We check for nono values, we will be traversing the tuple and checking if any value is none. If none value is present, return True otherwise return False.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1:

One method to solve the problem is by using check if any element of the tuple is none. This is done using any() method to check if any value in the tuple is none checked using a lambda function checking for nono comparison of each element wrapped by map().

# Python Program to check if the tuple 
# has any None Value

# Initializing and printing the tuple 
myTup = (4, 8, None, 2)
print("The elements of the tuple are " + str(myTup))

# Checking if tuple has any None Value 
hasNone = any(map(lambda ele: ele is None, myTup))

# printing result
if hasNone :
    print("The tuple contains None Value...")
else : 
    print("The tuple does not contain any None Value...")

Output:

The elements of the tuple are (4, 8, None, 2)
The tuple contains None Value...

Method 2:

One method to check if any element of the tuple is None or not is by using the all() method. The method checks whether each element is valid or not, if any None value is present the method will return false. We will check based on this method and invert the result of the method.

# Initializing and printing the tuple 

myTup = (4, 8, None, 2)
print("The elements of the tuple are " + str(myTup))

# Checking if tuple has any None Value 
hasNone = not all(myTup)

# Printing result
if hasNone :
    print("The tuple contains None Value...")
else : 
    print("The tuple does not contain any None Value...")

Output:

The elements of the tuple are (4, 8, None, 2)
The tuple contains None Value...

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to chunk tuples to N size... >>
<< Python program to perform comparison operation on ...