Select Page

How to Solve Python AttributeError: ‘bool’ object has no attribute ‘all’

by | Programming, Python, Tips

The AttributeError ‘bool’ object has no attribute ‘all’ occurs when you try to call the all() method on a Boolean. This error typically happens when comparing two lists. Suppose you are working with two lists of equal length and check for equality, for example, list1 == list2. In that case, you will get a single Boolean value because you are only checking if both are equal and not an element-by-element comparison like with NumPy ndarrays.

You can use the equality comparison operator without using all() to solve this error. If you want to perform an element-wise comparison, convert the lists to ndarrays using numpy.array(). Once you have ndarrays, you can make a new ndarray by comparing the two ndarrays, for example,

comparison = array1 == array2 and then call all() on the comparison ndarray: comparison.all()

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: ‘bool’ object has no attribute ‘all’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part of the error ‘bool’ object has no attribute ‘all’ tells us that the Boolean object we are handling does not have all() as an attribute. The all() method is a built-in Python method that returns True if all items in an iterable are true. Otherwise, it returns False.

NumPy also has a built-in all() method, which checks whether all array elements along a given axis evaluate to True. NumPy ndarrays also have the all() method, which returns True if all elements evaluate to True.

When we compare two lists, the result is a single Boolean, not a list of Booleans for each element comparison. We can verify this with the example below:

lst1 = [2, 4, 6, 8]
lst2 = [2, 3, 5, 8]

comparison = lst1 == lst2

print(comparison)
False

If we try to call all() on the comparison variable, we call the method on a Boolean, which does not have all() as an attribute.

Example

Let’s look at an example where we want to check if two lists are equal.

# Define lists

lst = [4, 7, 12, 17, 23, 44]

lst2 = [4, 3, 2, 1, 17, 44]

# Check lists are equal length

if len(lst) != len(lst2):

    print('Lists are not of equal length')

else:

    # Check if lists are equal

    if (lst == lst2).all():

        print('All elements are equivalent')

    else:

        print('Not all elements are equivalent')

In the above code, we check if the lists are of equal length, perform an equality comparison operation, and then call all() on the comparison result. Let’s run the code to see the result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [15], in <cell line: 9>()
     11     print('Lists are not of equal length')
     13 else:
     14 
     15     # Check if lists are equal
---> 17     if (lst == lst2).all():
     19         print('All elements are equivalent')
     21     else:

AttributeError: 'bool' object has no attribute 'all'

The error occurs because we call the all() method on the result of the equality comparison operation, which is a Boolean.

Solution #1: Use the Equality Operator Alone

Comparing lists is, in essence, doing what all() does; we get a single Boolean that is True if all elements are equal between the two lists. Otherwise, False. Therefore, we do not need to use all() if we are handling two lists. Let’s look at the revised code:

# Define lists

lst = [4, 7, 12, 17, 23, 44]

lst2 = [4, 3, 2, 1, 17, 44]

# Check if lists are equal length

if len(lst) != len(lst2):

    print('Lists are not of equal length')

else:

    # Check if lists are equivalent

    if lst == lst2:

        print('All elements are equivalent')

    else:

        print('Not all elements are equivalent')

Let’s run the code to see what happens:

Not all elements are equivalent

Let’s see the result when we use two identical lists:

lst = [4, 7, 12, 17, 23, 44]

lst2 = [4, 7, 12, 17, 23, 44]

if len(lst) != len(lst2):

    print('Lists are not of equal length')

else:

    if lst == lst2:

        print('All elements are equivalent')

    else:

        print('Not all elements are equivalent')
All elements are equivalent

Solution #2: Compare to NumPy arrays Using Numpy all()

If we want to make an element-wise comparison, we can use NumPy arrays instead of lists.

import numpy as np

# Define two arrays

arr1  = np.array([4, 7, 12, 17, 23, 44])

arr2 = np.array([4, 7, 12, 17, 23, 44])

# Check for equal length

if len(arr1) != len(arr2):

    print('Arrays are not of equal length')

else:

    # Comparison array

    comparison = (arr1 == arr2)

    # Call all() on array

    if comparison.all():

         print('All elements are equivalent')

    else:

        print('Not all elements are equivalent')

In the above code, we define two NumPy arrays and check they are equal in length. Then, we use the equality comparison operator to compare the two NumPy arrays and generate a new array object. Next, we call ndarray.all() on the new array object, which will return True if the two NumPy arrays are equivalent; otherwise, False. Let’s run the code to see the result.

All elements are equivalent

We can also pass the comparison array to the built-in NumPy all() method or the Python all() method. Let’s look at the revised code:

import numpy as np

# Define two arrays

arr1  = np.array([4, 7, 12, 17, 23, 44])

arr2 = np.array([4, 7, 12, 17, 23, 44])

# Check for equal length

if len(arr1) != len(arr2):

    print('Arrays are not of equal length')

else:

    # Comparison array

    comparison = (arr1 == arr2)

    # Use Numpy.all() 

    if np.all(comparison):

         print('All elements are equivalent')

    else:

        print('Not all elements are equivalent')

Let’s run the code to see the result:

All elements are equivalent
import numpy as np

# Define two arrays

arr1  = np.array([4, 7, 12, 17, 23, 44])

arr2 = np.array([4, 7, 12, 17, 23, 44])

# Check for equal length

if len(arr1) != len(arr2):

    print('Arrays are not of equal length')

else:

    # Comparison array

    comparison = (arr1 == arr2)

    # Use built-in all() 

    if all(comparison):

         print('All elements are equivalent')

    else:

        print('Not all elements are equivalent')

Let’s run the code to see the result:

All elements are equivalent

Using any() to Compare NumPy Arrays

Because we are comparing NumPy arrays, we can use the NumPy.ndarray.any() method to check if any elements between the two NumPy arrays are equivalent. The any() method returns True if any are equivalent; otherwise, it returns False. Let’s look at the revised code:

import numpy as np

# Define two arrays

arr1  = np.array([4, 7, 12, 17, 23, 44])

arr2 = np.array([0, 7, 1, 2, 56, 100])

# Check for equal length

if len(arr1) != len(arr2):

    print('Arrays are not of equal length')

else:

    # Comparison array

    comparison = (arr1 == arr2)

    # Call any() on array

    if comparison.any():

         print('At least one element is equivalent')

    else:

        print('None of the elements are equivalent')

Let’s run the code to see the result:

At least one element is equivalent

We can also pass the comparison array to the built-in NumPy any() method or the Python any() method.

Summary

Congratulations on reading to the end of this tutorial. The AttributeError: ‘bool’ object has no attribute ‘all’ can occur when trying to compare two lists and calling the all() method on the result of the comparison. To solve this error, you can convert the list to NumPy arrays, perform the comparison to get an array of Booleans and then call the all() method on this array. You can also pass the array to the built-in NumPy all() method or Python all() method.

For further reading on using any() and all(), go to the article: How to Solve Python ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!