Check if all values are True in a List in Python

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

Table Of Contents

Using for-loop to check if List has only True values

Iterate over all the elements of a list using a for loop, and for each element check if it is True or not. As soon as you find an element that is not True, break the loop, because it means all values in List are not True. But also make sure that list is not empty.

Let’s see a complete example,

listOfElements = [True, True, True, True, True]

result = True

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

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

Output:

Yes, all values are True in a List in Python

There were only True values in the List.

Using all() method to check if List has only True values

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 True or not, just pass the list to the any() function. If it returns True, then it means list has only True values. But also make sure that list is not empty. Let’s see a complete example,

listOfElements = [True, True, True, True, True]

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

Output:

Yes, all values are True in a List in Python

There were only True values in the List.

Using NumPy to check if List has only True values

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. We can pass our list object to numpy.all() function, and if it returns True, then it means our list has only True values. Let’s see an example,

import numpy as np

listOfElements = [True, True, True, True, True]

# Create a NumPy Array from a List
elements = np.array(listOfElements)

# Check if all values are True in a List in Python
if np.all(elements) and len(listOfElements) > 0:
    print('Yes, all values are True in a List in Python')
else:
    print('No, all values are not True in a List in Python')

Output:

Yes, all values are True in a List in Python

There were only True values in the List.

Using Set to check if List has only True values

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

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

Let’s see an example,

listOfElements = [True, True, True, True, True]

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

Output:

Yes, all values are True in a List in Python

There were only True values in the List.

Summary

We learned how to verify if a List has only True value 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