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 iterableThis 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
502: 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 True3: 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:
True4: 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
True5: 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 iterableConclusion
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.
- Typeerror unsupported operand type s for str and float
- Typeerror: ‘dict_keys’ object is not subscriptable
- Typeerror: object of type ndarray is not json serializable
Thank you very much for reading to the end of this article.
Understanding “object is not iterable” TypeErrors
Iteration in Python requires an object that implements the iterator protocol — __iter__ or __getitem__. Integers, None, and single objects do not. This error fires the moment you write “for x in y” or “list(y)” where y is not iterable.
Common triggers
- Iterating over an integer.
for i in 5fails. You wantfor i in range(5). - Iterating over None. Usually from a function that should have returned a list but didn’t.
- Unpacking a scalar.
a, b = some_func()fails if some_func returned a scalar. - Passing a generator that has already been exhausted. Generators yield values only once. Loop over them a second time and you get nothing.
- Confusing dict.keys() with list. dict_keys is iterable but not indexable —
keys[0]fails.
Diagnostic pattern
# BAD
def get_ids():
if not_available:
return None # implicit iterator break
return [1, 2, 3]
for i in get_ids(): # TypeError: 'NoneType' object is not iterable
print(i)
# GOOD — always return an iterable, even when empty
def get_ids():
if not_available:
return []
return [1, 2, 3]
Best practices
- Return an empty list, not None, when a function should yield a sequence.
- Convert once to list if you need to iterate twice:
items = list(generator). - Use isinstance if you accept multiple types:
if isinstance(x, (list, tuple, set)):
Official documentation
Frequently Asked Questions
What is Python TypeError and what causes it?
TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.
How do I quickly debug a Python TypeError?
Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.
Should I catch TypeError or let it propagate?
For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.
How do I prevent TypeError in production?
Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.
Where can I find more TypeError fixes?
Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.
