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)Traceback (most recent call last): File "main.py", line 2, in <module> TypeError: 'int' object is not iterable
Fixed code
Use range(n) to make the numbers from 0 to n - 1.
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])Traceback (most recent call last): File "main.py", line 2, in <module> TypeError: 'int' object is not iterable
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)0 x 1 y 2 z 0 x 1 y 2 z
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))Traceback (most recent call last): File "main.py", line 3, in <module> TypeError: 'int' object is not iterable
Fixed code
To add a few numbers, just use +, or wrap them in square brackets to make a list.
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)Traceback (most recent call last): File "main.py", line 2, in <module> TypeError: 'NoneType' object is not iterable
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.
What to check when you see this error
- On the line where the error happened (the number after
line), look at what comes afterin. If the line has nofor, look inside the parentheses of functions likesum(),list()andmax(). - If that value is a number, think about whether it should be wrapped in
range(). - If the word in quotes is
'NoneType', check that the function that produced the value actually returns something (return). - 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
- Python 'int' object is not subscriptable
- Python TypeError: 'int' object is not callable
- Python ValueError: not enough values to unpack
- Syntax: Python range(): Start, Stop and Step
- Syntax: Python for Loop
- Syntax: Python enumerate(): Loop with Index and Value
- Syntax: Python sort() vs sorted(): Sort a List
- Learn it step by step: Python Loops
- All Python error messages
- Online Python interpreter