TryBugHunt

TypeError: 'int' object is not iterable

TypeError: 'int' object is not iterable in Python means you tried to loop over a single number. A for loop, sum(), list() and in all need a collection of items, and a number is just one value.

PythonPattern: Type errors

How do I fix TypeError: 'int' object is not iterable?

TypeError: 'int' object is not iterable means you tried to loop over a single number, as in for i in 5 or for i in len(items). A for loop, sum() and list() need a collection. Wrap the count in range(), like for i in range(n), or loop over the list itself.

  1. 1. Look after inCheck what follows in, or what is inside sum(), max() or list(), on the failing line. Print its type.
  2. 2. Wrap counts in range()If the value is a count, write for i in range(n). Better still, loop directly over the list.
  3. 3. Check unpackingFor a, b = value, the value must hold two items. Make the function return both, like return high, low.

What does TypeError: 'int' object is not iterable mean?

"Iterable" means "something you can go through one item at a time", like a list, string or range. for i in 5 has no items to go through. The fix is almost always range(5), which produces 0, 1, 2, 3, 4. Related messages: "cannot unpack non-iterable int object" (a, b = 5) and "argument of type 'int' is not a container or iterable" (3 in 5; Python 3.13 and older say "is not iterable").

Why does TypeError: 'int' object is not iterable happen?

1. Looping over a number instead of a range

for needs a sequence. range(n) gives the numbers 0 to n - 1.

Breaks

n = 5
for i in n:
    print(i)

Works

n = 5
for i in range(n):
    print(i)

2. Looping over len() of a list

len(items) is one number. Loop over range(len(items)), or over the list itself.

Breaks

for i in len(items):
    print(items[i])

Works

for item in items:
    print(item)

3. Passing a number to sum(), max() or list()

These take a collection. sum(5) has nothing to add up.

Breaks

total = sum(price)

Works

total = sum(prices)   # the list, not one price

4. Unpacking a number into two names

a, b = value needs value to hold two items.

Breaks

def stats(nums):
    return max(nums)

high, low = stats([3, 9, 1])

Works

def stats(nums):
    return max(nums), min(nums)

high, low = stats([3, 9, 1])

How to find it in your own code

Checklist: 1) Look at what comes after in, or inside sum()/list()/max() on the failing line. 2) print(type(x), x) for it. 3) If it is a count, wrap it in range(). 4) If you expected a list, find where the variable was replaced by a number, or which function returned one value instead of several.

Practise type errors

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all type errors →