TypeError: 'int' object is not iterable — decoded
Exception: TypeErrorCategory: runtimeRaised at: when a for loop asks a number for items
What this message means
Iterable means: a thing that can hand out its items one at a time — a list, a string, a tuple, a dict, a range, a file. A for loop works by asking the thing after in for item after item until it runs out. An int has no items to hand out, so the loop cannot even start, and Python raises this TypeError.
The message names the object that failed to cooperate: whatever sits after the in (or inside sum(...), list(...), for ... in) was a plain number when the loop needed a sequence.
Traceback (most recent call last):
File "main.py", line 1, in <module>
for i in 5:
^
TypeError: 'int' object is not iterable
A real example
count = 5
for i in count: # for i in 5 — there is nothing to hand out
print(i)
The intent is "run five times", and the tool for that intent is range() — which *is* iterable because it produces the numbers one by one:
for i in range(5): # 0, 1, 2, 3, 4
print(i)
The everyday lines that raise it
for item in total:— you meantrange(total)or you meant the *list* (for item in items:) and grabbed the count variable by mistake.sum(n)ormax(n)wherenis one number — those functions iterate their argument; pass the collection, not a member of it.list(5)/tuple(5)— converting from a sequence works, converting from a count does not.a, b = 5— unpacking is iteration too; it needs two items, not the number two. Python raises a relatedTypeErrorbefore the loop even begins.- A function returned a count —
len(items)used whereitemsitself was intended, one letter of difference at 2 a.m.
The fix, step by step
- Read the character after
in— that is the value that was anint. - Ask what you actually wanted: to repeat something N times, or to visit each item of a collection?
- For repetition, wrap the count in
range()— and remember it starts at 0 and excludes the stop value. - For visiting items, use the collection itself —
for item in items:. - If the value came from a function, check its return: maybe you stored
len()or a sum where the list was meant.
items = ["pen", "ink", "pad"]
for i in range(len(items)): # positions 0..2 — use when you need i
print(i, items[i])
for item in items: # the items themselves — use when you do not
print(item)
Pythonic style prefers the second loop: take the item directly and let the index go unless you genuinely need it.
Related messages worth knowing
- TypeError: the parent guide — every famous wording of this exception on one page.
- TypeError: int is not subscriptable — the sibling message when the same number sits in front of brackets instead.
- ValueError: too many values to unpack — what iteration says when it yields more items than you have names for.
Fix it interactively
These free problems drill the loop-vs-count decision — repair them in the browser, no setup:
Practice more Python bugs →🇮🇳 Hindi में समझें
for loop उस चीज़ से एक-एक करके items मांगता है — list, string, tuple, range सब दे सकती हैं, पर एक number (int) के पास देने के लिए कोई item होता ही नहीं। इसलिए for i in 5: fail होता है। अगर मकसद "5 बार चलाना" है तो range(5) लिखो; अगर मकसद "हर item पर चलाना" है तो list वाला variable लिखो, count वाला नहीं। याद रखो: range(5) 0 से शुरू होकर 4 तक जाता है — 5 शामिल नहीं।