How To Check If Any Item In A List Is None Using Python

Check if any item in a list is None using Python

To check if any item in a list is None in python, we can use the membership Operators or list comprehension.

Check if any item in a list is None using Python

In Python, ‘None’ is a specific object indicating that the object is missing the presence of a value. The Python Null object is the singleton None. 

Note: constant ‘None’ is defined as False, so when combined using the same logical operator or always returns False.

To check if any item in a list is None, we have the following ways:

Use the membership Operators

The membership operators in Python include ‘in’ and ‘not in’.

The ‘in’ operator will return True if the specified argument occurs in a set and vice versa.

The ‘not in’ operator will return True if the specified argument is not in the set and vice versa.

Example:

  • Initializes a list of both strings and None.
  • Use the membership operator to check if the value None appears in the list.
# A list consisting of strings and None
myList = ["Learn", None, "Share", None, None, "IT"] 

# Use the in operator to check if any item in a list is None
if None in myList:
    print('The list contains the value None')
else:
    print('The list does not contain the value None')

Output:

The list contains the value None

Or you can use the ‘not in’ operator to check.

Example:

# A list consisting of strings and None
myList = ["Learn", None, "Share", None, None, "IT"] 

# Use the in operator to check if any item in a list is None
if None not in myList:
    print('The list does not contain the value None')
else:
    print('The list contains the value None')

Output:

The list contains the value None

The program still outputs that command line because there is a None value in the list.

Use list comprehension

Example:

  • Initializes a list of both strings and None.
  • Use the list comprehension to output the None values ​​contained in the list.
# A list consisting of strings and None
myList = ["Learn", None, "Share", None, None, "IT"] 

# Use list comprehension to output None values in the list
result = [x for x in myList if x is None]

print('The value None appears in the list:', result)

Output:

The value None appears in the list: [None, None, None]

Use the any() function

Syntax:

any(iterable)

Parameters:

  • iterable: Can be a list, string, dictionary,… in Python.

The any() function returns:

  • True if at least one element in an iterable is True.
  • False if all elements in iterable are False or iterable is empty.

Example:

  • Initialize a list of both strings and None.
  • Use the any() function to check if the value None appears in the list.
# A list consisting of strings and None
myList = ["Learn", None, "Share", None, None, "IT"] 

# Use the any() function to output None values in the list
result = any(x is None for x in myList)

print('The value None appears in the list:', result)

Output:

The value None appears in the list: True

Summary

Above are some ways I often use to check if any item in a list is None using Python. After reading this article, you may have better ways for this topic.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *