Ways To Check If All Items In A List Are None In Python

Check if all items in a list are None in Python

In this article, we look at how to use the all() function to check if all items in a list are None in Python. In addition, we can use many other functions to do that task. Now, let’s explore and see how these functions work.

Check if all items in a list are None in Python

Using all() function

The all() function in Python will return True when every element of the iterable is True. If any element is False or has a value similar to False, it’ll return False.

Syntax:

all(iterable)

Parameter:

  • iterable: the object can be a string, tuple, set, or list.

To determine whether all items in a list are None, we use a list comprehension to iterate through the elements in the given list and check each one with the is None condition, returning True if the element is None. Then we use all() to see if they are all True; if they are, then all of the items in the original list are None.

list1 = [None, None, 'learnshareit']
list2 = [None, None, None]

def areNone(iterable):
  # Use the is operator to check if the item is None
  isNoneList = [item is None for item in iterable]

  # if isNoneList contains only True, all() will return True
  if all(isNoneList):
      print("All items are None")
  else:
      print("At least one non-None value exists")

areNone(list1)  # At least one non-None value exists
areNone(list2)  # All items are None

Output:

At least one non-None value exists
All items are None

Using any() function

The any() function takes an iterable (like a list) and returns True if any elements are True.

Syntax

any(iterable)

Parameter:

  • iterable: the object can be a string, tuple, set, or list.

We also use list comprehension to check each one with the is not None condition. If the element isn’t None, it returns True. Then we use any() to see if any new list items are True. If yes, at least one non-None value is present in the original list. Consider the following example.

list1 = [None, None, 'learnshareit']
list2 = [None, None, None]

def areNone(iterable):
  # Use is not operator to check if the item is not None
  isNoneList = [item is not None for item in iterable]

  # if isNoneList contains any True, it means at least one non-None value exists
  if any(isNoneList):
      print("At least one non-None value exists")
  else:
      print("All items are None")

areNone(list1)  # At least one non-None value exists
areNone(list2)  # All items are None

Output:

At least one non-None value exists
All items are None

Using count() function

The count() function is a handy function in Python that allows you to quickly find the number of occurrences of an element in a list.

Syntax:

list.count(element)

Parameter:

  • element: the value you want to check.

Let’s start by passing the value ‘None’ to the count() function to determine how many None values are in our list. We then compare this to the length of our list; if they’re equal, all of the items in our list are None. If it’s less than the length of our list, there must be at least one non-None value.

list1 = [None, None, 'learnshareit']
list2 = [None, None, None]

def areNone(iterable):
  # If the number of None equals the length of the iterable, return True
  if iterable.count(None) == len(iterable):
      print("All items are None")
  else:
      print("At least one non-None value exists")

areNone(list1)  # At least one non-None value exists
areNone(list2)  # All items are None

Output:

At least one non-None value exists
All items are None

Summary

To save time and optimize your code, use the all() function to quickly Check if all items in a list are None. We hope this article has been helpful to you on your path to becoming a Python developer. 

Have a great day!

Maybe you are interested:

Leave a Reply

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