Check if all values in List are False in Python

In this article, we will discuss different ways to check if all values are False in a Python List.

Table Of Contents

Using for-loop to check if all values in List are False

Iterate over all the elements of a list using a for loop, and for each element check if it is False or not. As soon as you find an element that is not False, break the loop, because it means there is at least a value in the list that does not evaluates to False . But also make sure that list is not empty. Let’s see a complete example,

listOfElements = [False, False, False, False, False, False]

result = True

# Check if all values are False in a List in Python
for elem in listOfElements:
    if elem != False:
        result = False
        break

if len(listOfElements) > 0 and result:
    print('Yes, all values are False in a List in Python')
else:
    print('No, all values are not False in a List in Python')

Output:

Yes, all values are False in a List in Python

There were only False values in the List.

Using all() method to check if all values in List are False

Python provides a function all(). It accepts an iterable sequence as an argument and returns True if all the elements in that sequence evaluates to True. Now, to check if all elements in a List are False or not, create a boolean list of same size. Each True value in this new boolean list represents the corresponding False Value in the original List. Then pass this new boolean list to the any() function. If it returns True, then it means the original list has only False values. But also make sure that list is not empty. Let’s see a complete example,

listOfElements = [False, False, False, False, False, False]

# Check if all values are False in a List in Python
if all(elem == False for elem in listOfElements) and len(listOfElements) > 0:
    print('Yes, all values are False in a List in Python')
else:
    print('No, all values are not False in a List in Python')

Output:

Yes, all values are False in a List in Python

There were only False values in the List.

Using NumPy to check if all values in List are False

The NumPy module has a function all(). It accepts an array like sequence as an argument and returns True if all the elements in that sequence evaluates to True. Now, to check if List has only False values, we will create a boolean NumPy Array of same size. A value in the boolean array will be True, if the corresponding value in the original list is False. Then pass our boolean NumPy Array object to the numpy.all() function, and if it returns True, then it means our original list has only False values. Let’s see an example,

import numpy as np

listOfElements = [False, False, False, False, False, False]

# Create a NumPy Array from list
numbers = np.array(listOfElements)

# Check if all values in array are zeros
if np.all(numbers == False) and len(listOfElements) > 0:
    print('Yes, List contains all False values')
else:
    print('No, List does not contains all False values')

Output:

Yes, all values are False in a List in Python

There were only False values in the List.

Using Set to check if all values in List are False

In Python, a Set is a kind of data structure that contains only unique elements. So, we can convert our list to a Set and check following conditions,

  • Set size should be 1.
  • First element of set should be equal to first element of list and that should evaluate to False.

If both the conditions are True, then it means our list has only False values. We can verify both the conditions by comparing the set with {False}.

Let’s see an example,

listOfElements = [False, False, False, False, False, False]

# Check if all values are True in a List in Python
if set(listOfElements) == {False}:
    print('Yes, all values are False in a List in Python')
else:
    print('No, all values are not False in a List in Python')

Output:

Yes, all values are False in a List in Python

There were only False values in the List.

Summary

We learned how to verify of a List has only False values in Python. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top