Typeerror: ‘bool’ object is not iterable

The “typeerror: ‘bool’ object is not iterable” is an error message in Python.

If you are wondering how to resolve this type error, keep reading.

In this article, you’ll get the solution that will fix the “bool object is not iterable” type error.

Aside from that, we’ll give you some insight into this error and why it occurs in Python scripts.

What is ‘bool’ object?

A ‘bool’ object is a data type in Python that represents a boolean value.

“Boolean” value represents one of two values: True or False.

These values are used to represent the truth value of an expression.

For instance, if you want to compare two values using a comparison operator such as (<, >, !=, ==, <=, or >=), the expression is evaluated.

And Python returns a “boolean” value (True or False) indicating whether the comparison is true.


Moreover, “bool” objects are often used in conditional statements and loops to control the flow of a program.


However, it’s important to note that “boolean” values and operators are not sequences that can be iterated over.

What is “typeerror: ‘bool’ object is not iterable”?

The “typeerror: ‘bool’ object is not iterable” occurs when you try to iterate over a boolean value (true or false) instead of an iterable object (like a list, string or tuple).

For example:

my_bool = True

for sample in my_bool:
    print(sample)

If you run this code, it will throw an error message since a boolean value is not iterable.

TypeError: 'bool' object is not iterable

This is not allowed because “boolean value” is not a collection or sequence that can be iterated over.

How to fix “typeerror: ‘bool’ object is not iterable”?

Here are the following solutions that will help you to fix this type error:

1: Assign an iterable object to the variable

Rather than assigning a boolean value to the variable. You can assign an iterable object such as a list, string, or tuple.

my_list = [10, 20, 30, 40, 50]
for sample in my_list:
    print(sample)

As you can see in this example code, we assign a list to the variable my_list and iterate over it using a for loop.

Since a list is iterable, this code will run smoothly.

Output:

10
20
30
40
50

2: Use if statement

When you need to check on a boolean value, you can simply use an if statement instead of a for loop.

my_bool = True
if my_bool:
    print("This example is True")
else:
    print("The example is False")

In this example code, we check if the boolean value “my_bool” is True using an if statement.

If the value is True, then it will print a message indicating that “This example is True”.

Otherwise, it will print a different message.

Output:

This example is True

3: Convert the boolean value to an iterable object

When you need to iterate over a boolean value, you just have to convert it to an iterable object, such as a list, string, or tuple.

my_bool = True
my_list = [my_bool]
for sample in my_list:
    print(sample)

Output:

True

4: Use generator expression

When you need to iterate over multiple boolean values, you can use a generator expression to create an iterable object.

my_bools = [True, False, True]
result = (x for x in my_bools if x)
for sample in result:
    print(sample)


As you can see in this example code, we use a generator expression to create an iterable object.

That contains only the True values from the list my_bools.


Then iterate over this iterable object using a for loop.

Output;

True
True

5: Use the isinstance() function

Before iterating over a variable, you can check its type using the isinstance() function to ensure it is an iterable object.

my_var = True
if isinstance(my_var, (list, tuple, str)):
    for sample in my_var:
        print(sample)
else:
    print("my_var is not iterable")

Here, we use the isinstance() function to check if the variable my_var is an instance of a list, tuple, or string.

Since it is not, it will print a message rather than try to iterate over it.

Output:

my_var is not iterable

Conclusion

In conclusion, the “typeerror: ‘bool’ object is not iterable” occurs when you try to iterate over a boolean value (True or False) instead of an iterable object (like a list, string or tuple).

Luckily, this article provided several solutions above so that you can fix the “bool object is not iterable” error message.

We are hoping that this article provided you with sufficient solutions to get rid of the error.

You could also check out other “typeerror” articles that may help you in the future if you encounter them.

Thank you very much for reading to the end of this article.