← Back

Home › Errors › TypeError

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

The fix, step by step

  1. Read the character after in — that is the value that was an int.
  2. Ask what you actually wanted: to repeat something N times, or to visit each item of a collection?
  3. For repetition, wrap the count in range() — and remember it starts at 0 and excludes the stop value.
  4. For visiting items, use the collection itself — for item in items:.
  5. 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

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 शामिल नहीं।

पूरी Hindi explanation पढ़ें →