🔧 Error Fixes
· 1 min read

Python TypeError: 'int' Object Is Not Iterable — How to Fix It


TypeError: 'int' object is not iterable

You’re trying to loop over a number, but you can only loop over things like lists, strings, ranges, and tuples.

Fix 1: Use range() in for loops

# ❌ Can't iterate over a number
for i in 5:
    print(i)

# ✅ Use range()
for i in range(5):
    print(i)  # 0, 1, 2, 3, 4

Fix 2: You’re passing an int to sum(), list(), join(), etc.

# ❌ sum() expects an iterable
total = sum(5)

# ✅ Pass a list
total = sum([1, 2, 3, 4, 5])
# ❌ join() expects an iterable of strings
result = ", ".join(42)

# ✅ Pass a list
result = ", ".join(["42"])

Fix 3: A function returns an int instead of a list

def get_items():
    count = len(some_list)
    return count  # Returns int, not the list!

# ❌ Trying to iterate over the return value
for item in get_items():  # TypeError!
    print(item)

# ✅ Return the actual list
def get_items():
    return some_list

Fix 4: Unpacking a single integer

# ❌ Can't unpack an int
a, b = 5

# ✅ Unpack an iterable
a, b = 5, 10
a, b = [5, 10]

How to debug

Check the type of what you’re iterating over:

data = get_something()
print(type(data))  # <class 'int'> — that's the problem
📘