Last modified: Apr 24, 2026 By Alexander Williams
Fix 'bool' Not Iterable TypeError
You are coding in Python and suddenly you see a red error: TypeError: argument of type 'bool' is not iterable. This error confuses many beginners. It happens when you try to loop over or check membership on a boolean value like True or False. This article explains why this error occurs and how to fix it. We will use simple examples and clear steps.
Understanding this error is important for writing clean Python code. It often appears in conditions, loops, or when using the in operator incorrectly. Let's break it down.
What Does This Error Mean?
The error means you are treating a boolean value as if it were a sequence. Python expects a list, string, tuple, or other iterable when you use the in keyword. A boolean (True or False) is not iterable. You cannot check if an item exists inside a boolean.
For example, this code triggers the error:
# Wrong code: checking membership on a boolean
my_bool = True
if 5 in my_bool: # Error! 'bool' is not iterable
print("Found")
The in operator expects an iterable like a list or string. Here, my_bool is True, which is a boolean. Python cannot iterate over it. The same error happens if you try a for loop over a boolean.
Common Scenarios That Cause This Error
This error often appears in three main situations. Knowing them helps you spot the bug fast.
1. Using in With a Boolean Variable
You might accidentally assign a boolean to a variable and later use it with in. This happens when a function returns True or False instead of a collection.
# Example: function returns bool instead of list
def check_data():
return True # Should return a list
my_data = check_data()
if "apple" in my_data: # Error!
print("Found apple")
In this case, check_data() returns a boolean. The fix is to return a list or check the logic of your function.
2. Accidentally Using == Instead of in
Sometimes you mix up comparison operators. You might write if x in y when y is a boolean. This often happens after a condition check.
# Confusing condition with membership
is_valid = (10 > 5) # This is True
if 10 in is_valid: # Error!
print("Valid")
Here, is_valid is a boolean. You cannot use in on it. The correct approach is to check the condition directly: if is_valid:.
3. Forgetting That a Function Returns a Boolean
Many Python functions return booleans. For example, str.isalpha() returns True or False. If you try to use in on its result, you get the error.
# Using in on a boolean result
text = "Hello123"
if "abc" in text.isalpha(): # Error! isalpha() returns bool
print("All letters")
The str.isalpha() method returns a boolean. You should check the result directly: if text.isalpha():.
How to Fix the Error
The fix depends on what you intended. Below are three common solutions.
Solution 1: Check the Variable Type First
Use type() or isinstance() to confirm the variable is iterable before using in.
# Safe way: check type before membership
my_var = True
if isinstance(my_var, (list, str, tuple)):
if 5 in my_var:
print("Found")
else:
print("Variable is not iterable")
This prevents the error by verifying the variable is a sequence. It is a defensive programming technique.
Solution 2: Correct the Logic
Often the error comes from a logic mistake. If you meant to check a condition, use the boolean directly.
# Correct: use boolean directly
is_valid = (10 > 5)
if is_valid: # No 'in' needed
print("Valid")
If you need to check membership, ensure the variable is a list, string, or other iterable.
Solution 3: Return the Right Data Type
If a function returns a boolean by mistake, change it to return a list or other iterable.
# Fix function to return a list
def get_fruits():
return ["apple", "banana", "cherry"] # Return list, not bool
my_fruits = get_fruits()
if "apple" in my_fruits: # Works fine
print("Found apple")
This is the most common fix when the error comes from a custom function.
Example With Full Code and Output
Let's see a complete example that reproduces the error and fixes it.
# Reproducing the error
def get_status():
return True # Wrong data type
status = get_status()
# This line causes TypeError
try:
if "active" in status:
print("Active")
except TypeError as e:
print(f"Error: {e}")
# Fixed version
def get_status_fixed():
return ["active", "inactive"] # Return list
status_fixed = get_status_fixed()
if "active" in status_fixed:
print("Active status found")
Error: argument of type 'bool' is not iterable
Active status found
The first attempt fails because status is a boolean. The fixed version works because it returns a list.
Best Practices to Avoid This Error
Follow these tips to prevent the TypeError: argument of type 'bool' is not iterable in your code.
- Always check the return type of functions before using
in. - Use
isinstance()to validate data types in critical code. - Write unit tests to catch such errors early.
- Use type hints in Python to document expected types.
- Read error messages carefully — they tell you the exact line.
For a deeper understanding of common Python errors, check out our guide on Python TypeError: Causes and Fixes. It covers many similar issues.
Conclusion
The TypeError: argument of type 'bool' is not iterable is a common Python mistake. It happens when you use the in operator or a for loop on a boolean value. The fix is simple: ensure your variable is an iterable like a list, string, or tuple. Check the return type of functions and correct your logic. With the examples and solutions in this article, you can now fix this error confidently. Keep coding and learning — every error teaches you something new.
For more debugging tips, visit our comprehensive guide on Python TypeError: Causes and Fixes.