Error guide · TypeError
TypeError: 'int' object is not iterable
A loop or function expected something with several items, but it was given a single number.
Example
for i in 5: print(i)
What Python says
TypeError: 'int' object is not iterable
As printed by Python 3.12. Other versions may word it slightly differently.
Why it happens
A for loop visits the items of a list, a string, a range or another collection. The number 5 is one value, so there is nothing to visit. range(5) produces the numbers 0 to 4. Functions such as sum(5), list(5) and max(5) fail for the same reason.
How to fix it
for i in range(5): print(i)
Output: 0 ⏎ 1 ⏎ 2 ⏎ 3 ⏎ 4
Tips
- Use
range(n)to repeat something n times. - To loop over the digits of a number, loop over
str(n). - Check that a variable is a list, and not a single value, before you loop over it.