{ }codelper

Error Messages › Python

Python TypeError: 'int' object is not iterable

TypeError: 'int' object is not iterable

A for loop can only go over things that give their values one at a time, like a list or a string. If you put a single number or None after in, there is nothing to take out, so you get this error. Most of the time, you forgot range().

Cause 1. You forgot range()

If you write "repeat n times" as for i in n:, it means "take values out of the number n".

Wrong code

n = 3for i in n:    print(i)
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
TypeError: 'int' object is not iterable

Edit and run it →

Fixed code

Use range(n) to make the numbers from 0 to n - 1.

n = 3for i in range(n):    print(i)
Output
0
1
2

Edit and run it →

Cause 2. You used len() without range()

len(a) is a single number: the length. To loop by index, you have to put that number into range().

Wrong code

a = ["x", "y", "z"]for i in len(a):    print(i, a[i])
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
TypeError: 'int' object is not iterable

Edit and run it →

Fixed code

Write range(len(a)). If you need both the index and the value, enumerate is simpler.

a = ["x", "y", "z"]for i in range(len(a)):    print(i, a[i])for i, v in enumerate(a):    print(i, v)
Output
0 x
1 y
2 z
0 x
1 y
2 z

Edit and run it →

Cause 3. You passed plain numbers to sum() or max()

sum() takes one list and adds up the values inside it. If you give it separate numbers, as in sum(3, 5), it tries to take values out of the first one, 3, and fails. list(5) and max(7) fail the same way.

Wrong code

a = 3b = 5print(sum(a, b))
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
TypeError: 'int' object is not iterable

Edit and run it →

Fixed code

To add a few numbers, just use +, or wrap them in square brackets to make a list.

a = 3b = 5print(a + b)print(sum([a, b]))print(max(a, b))
Output
8
8
5

Edit and run it →

max() and min() do accept two or more separate numbers, as in max(3, 5). They only fail when you give them a single one (max(7)).

Cause 4. You looped over None

If the error message is 'NoneType' object is not iterable, the value after in is None. a.sort() sorts the list in place and returns nothing, so for x in a.sort(): loops over None. The result of a function with no return is None too.

Wrong code

a = [3, 1, 2]for x in a.sort():    print(x)
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
TypeError: 'NoneType' object is not iterable

Edit and run it →

Fixed code

To loop over the sorted values directly, use sorted(a). Or put a.sort() on its own line first, then loop over a.

a = [3, 1, 2]for x in sorted(a):    print(x)
Output
1
2
3

Edit and run it →

What to check when you see this error

  1. On the line where the error happened (the number after line), look at what comes after in. If the line has no for, look inside the parentheses of functions like sum(), list() and max().
  2. If that value is a number, think about whether it should be wrapped in range().
  3. If the word in quotes is 'NoneType', check that the function that produced the value actually returns something (return).
  4. Trying to split one value into two variables, as in a, b = 5, gives a similar error (cannot unpack non-iterable int object).

Practice problems

See also