
The Problem
In Python, you’ll encounter the TypeError: '...' object is not iterable when you try to use a non-iterable object in a context that requires iteration, such as a for loop or an in operator. The '...' part of the error message will specify the type of the object, like int, float, or NoneType.
An iterable is an object that can return its members one at a time. Examples include lists, tuples, dictionaries, and strings. In contrast, single-value objects like numbers (integers, floats) or None are not iterable.
Examples of Error-Prone Code
1. Trying to Iterate Over a Number
A common mistake is trying to use a number directly in a for loop.
# This will raise TypeError: 'int' object is not iterable
for i in 123:
print(i)
The integer 123 is a single value, not a collection of items, so it cannot be iterated over.
2. Function Returns a Non-Iterable Value
This error also occurs if a function returns a single value (like None or a number) when an iterable (like a list) is expected.
def get_data():
# This function returns None if there's no data
return None
# The 'data' variable is None, which is not iterable
# This will raise TypeError: 'NoneType' object is not iterable
data = get_data()
for item in data:
print(item)
How to Fix It
1. Check if the Object is Iterable
When debugging, it’s crucial to check the value of the variable you are trying to iterate over. Use print() or a debugger to inspect it. You might find it’s None or another single value instead of the list or dictionary you were expecting.
A good practice is to design functions to return an empty iterable (e.g., [] or ()) instead of None when there are no results.
def get_data(condition):
if condition:
return ["apple", "banana"]
# Return an empty list instead of None
return []
data = get_data(False)
print(f"Data received: {data}") # Data received: []
# Iterating over an empty list is perfectly fine and won't cause an error.
for item in data:
print(item)
2. Wrap a Single Object in a List or Tuple
If you know you might be dealing with a single item but want to use a for loop, you can wrap it in a list or tuple to make it iterable.
my_variable = 123
# If my_variable is not a list, make it one
if not isinstance(my_variable, list):
my_variable = [my_variable]
for item in my_variable:
print(item)
# Output: 123
3. Use the range() Function for Numbers
If your intention was to iterate over a sequence of numbers, you should use the range() function.
# Iterate from 0 to 4
for i in range(5):
print(i)
# Output: 0, 1, 2, 3, 4
Conclusion
The TypeError: '...' object is not iterable arises from attempting to loop over an object that doesn’t support iteration. To fix this:
- Ensure the variable you are iterating over is an iterable type like a list, tuple, dictionary, or string.
- Design functions to return an empty list (
[]) or tuple instead ofNonefor “no result” cases. - If you need to iterate over a single item, wrap it in a list (
[item]). - To iterate over a sequence of numbers, use the
range()function.
Always being mindful of your variables’ types is key to avoiding this common error.
Professional Depth Check
For How to Fix Python TypeError: ‘…’ object is not iterable, the practical standard is not whether the reader can repeat one instruction once. Treat the topic as a reproducible debugging procedure: verify interpreter path, virtual environment, package version, and input file or data boundary before drawing a conclusion. The result should be written as a small decision record, because future readers need to know which fact was observed, which assumption was used, and which condition would change the answer.
Evidence That Makes the Guidance Reliable
Use objective evidence before changing a workflow. Good evidence includes python --version, python -m pip show, the full traceback, and a minimal script. If two pieces of evidence conflict, keep the conflict visible instead of smoothing it over. For example, a successful quick fix is still weak evidence if the same input, account, dependency, or device state has not been tested again. A durable article should help the reader distinguish a confirmed fix from a plausible fix.
Review Table
| Review Item | What To Confirm | Why It Matters |
|---|---|---|
| Scope | The exact case covered by this article | Prevents over-applying the advice |
| Baseline | The state before any change | Makes rollback and comparison possible |
| Change | The smallest action taken | Reduces hidden side effects |
| Result | The observed output after the change | Separates evidence from expectation |
| Recheck | When to revisit the conclusion | Keeps the post accurate over time |
Edge Cases and Failure Modes
The main risks are fixing the symptom while leaving the root cause, and mixing unrelated changes into the same test. When the situation involves production data, personal information, money, health, legal rights, or security recovery, the conservative path is to stop and collect evidence before applying a broad fix. The same title can describe very different cases, so the reader should compare their environment with the assumptions in the post before copying commands or decisions.
Maintenance Standard
Recheck this guidance after dependency, operating-system, or build-tool changes. A useful update does not need to rewrite the entire post; it should confirm whether the examples, links, commands, screenshots, and decision criteria still match current behavior. If the old conclusion remains valid, record the check date. If it changes, explain what changed and why the previous advice is no longer enough.
Practical Questions Before Acting
- What is the smallest observable signal that proves the problem or decision is real?
- Which source is official, and which part is local judgment?
- What should be captured before making changes?
- What result would show that the guidance did not apply?
- Who needs the record if the same issue appears again?
Related Reading
Continue with these related posts from the same topic area.
Leave a comment