Select Page

How to Solve Python TypeError: object of type ‘bool’ has no len()

by | Programming, Python, Tips

This error occurs when you pass a bool to a len() function call. The Python Boolean type is used to represent the truth value of an expression and has only two possible values: True and False. In Python, bool objects do not have a length.

You can solve the error by only passing iterable objects to the len() function. For example, you can get the length of a string. For example,

if len(string1) == len(string2):
    print('Strings are equal')

You can check what the type of an object is before calling len() using the type() function.

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


TypeError: object of type ‘bool’ has no len()

We raise a Python TypeError when attempting to perform an illegal operation for a specific type. In this case, the type is int.

The part ‘has no len()‘ tells us the map object does not have a length, and therefore len() is an illegal operation for the bool object.

Retrieving the length of an object is only suitable for iterable objects, like a list or a tuple.

The Python Boolean is a built-in data type used to represent the truth value of an expression and can have two values True or False.

The len() method implicitly calls the dunder method __len__() which returns a positive integer representing the length of the object on which it is called. All iterable objects have __len__ as an attribute. Let’s check if __len__ is in the list of attributes for an bool object and a list object using the built-in dir() method.

val = 64 % 2 == 0

print(type(val))

print('__len__' in dir(val))
<class 'bool'>
False

We can see that __len__ is not present in the attributes of the bool object.

lst = ["football", "rugby", "tennis"]

print(type(lst))

print('__len__' in dir(lst))
<class 'list'>
True

We can see that __len__ is present in the attributes of the list object.

Example

Let’s look at an example of trying to get the length of a bool object. First, we will define a function that compares two strings and returns a print statement.

def check_word_length(string):

    compare = "guitar"

    if len(string == compare):

        print(f'String {string} is equal length to {compare}')

    else:

        print(f'String {string} is not of equal length to {compare}')

Let’s define a string and pass it to the function call:

my_str = "cymbal"

check_word_length(my_str)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [19], in <cell line: 3>()
      1 my_str = "cymbal"
----> 3 check_word_length(my_str)

Input In [18], in check_word_length(string)
      1 def check_word_length(string):
      3     compare = "guitar"
----> 5     if len(string == compare):
      7         print(f'String {string} is equal length to {compare}')
      9     else:

TypeError: object of type 'bool' has no len()

The error occurs because the expression string == compare returns a bool value. Relational operations like checking if two operands are equal always return either True or False.

Therefore, when we call the len() function, we are trying to get the length of a bool.

Solution

We can solve this error by comparing the lengths of the two strings. We need to have two separate len() calls, one for each string on either side of the equality operator.

Let’s look at the revised code:

def check_word_length(string):

    compare = "guitar"

    if len(string) == len(compare):

        print(f'String {string} is of equal length to {compare}')

    else:

        print(f'String {string} is not of equal length to {compare}')

Let’s define a string object and pass it as an argument to the check_word_length function call:

my_str = "cymbal"

check_word_length(my_str)

Let’s run the code to get the result:

String cymbal is of equal length to guitar

We successfully checked if two strings were equal in length and printed the result to the console.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on the has no len() TypeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python, which provides the best, easy-to-use online courses.