Python program to check if the tuple has any none value

In this program, we are given a tuple with some values. We need to create a Python program to check if the tuple has any None Value.
Submitted by Shivang Yadav, on December 23, 2021

While working with complex collections or collections in general in python, managing empty or none values is very important. Checking for all valid elements before working to avoid errors. Here, we will see a Python Program to check if the tuple has any None Value.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

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.

In Python, the series of these tasks can be performed using multiple built-in methods.

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...

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.