TypeError: 'int' object is not iterable (Fix)
Fix the Python error TypeError: 'int' object is not iterable. This page explains what the error means, why it happens, and the most common ways to correct your code.
Quick fix
number = 5
# Wrong
for item in number:
print(item)
# Right: loop over a range
for item in range(number):
print(item)
An integer is a single number, not a collection. If you want to loop a certain number of times, use range(). If you meant to loop over values, make sure the variable is a list, tuple, string, or another iterable.
What this error means
Python raises this error when your code tries to use an integer in a place where an iterable is required.
An iterable is an object you can loop over one item at a time.
Common iterables include:
listtuplestringdictionarysetrange
An int is different. It is just one value, so Python cannot iterate over it.
When this error usually happens
This error often appears in situations like these:
- Using
for item in my_number - Using
inwith a number, such as'a' in 5 - Passing an integer to a function that expects an iterable
- Accidentally replacing a list variable with an integer
Example that causes the error
Here is a simple example:
for item in 10:
print(item)
This produces:
TypeError: 'int' object is not iterable
Why?
Because 10 is one value, not a sequence of values. A for loop needs something it can move through one item at a time.
Fix 1: Use range() when you want to loop a number of times
This is the most common fix for beginners.
If you want to repeat something 5 times, do not loop over 5 directly. Loop over range(5) instead.
for i in range(5):
print(i)
Output:
0
1
2
3
4
range(5) creates an iterable that produces the numbers 0, 1, 2, 3, 4.
If you are still learning loops, see Python for loops explained and the range() function explained.
Fix 2: Use a real iterable when you want to loop over values
If you want to loop over several values, store those values in a real iterable such as a list, tuple, or string.
Wrong:
numbers = 123
for n in numbers:
print(n)
Right:
numbers = [1, 2, 3]
for n in numbers:
print(n)
Output:
1
2
3
If you are unsure what your variable contains, print its type:
numbers = 123
print(numbers)
print(type(numbers))
Output:
123
<class 'int'>
You can learn more about checking types with the type() function explained.
Fix 3: Convert data before iterating if needed
Sometimes you really do want to loop over the digits of a number.
In that case, convert the number to a string first:
number = 123
for digit in str(number):
print(digit)
Output:
1
2
3
This works because str(number) becomes "123", and strings are iterable.
Use this only when looping over characters or digits makes sense. If needed, see how to convert int to string in Python.
How to debug this error
When this error happens, check the value and type of the variable right before the line that fails.
Useful debug statements:
print(value)
print(type(value))
print(isinstance(value, int))
print(isinstance(value, (list, tuple, str, dict, set, range)))
A simple debugging example:
value = 7
print(value)
print(type(value))
for item in value:
print(item)
Output before the error:
7
<class 'int'>
Debugging steps:
- Print the variable before the failing line
- Use
type(variable)to confirm its type - Check where the variable was last assigned
- Look for places where a list or string was overwritten with an integer
Common beginner confusion
A few similar-looking values behave very differently:
range(5)is iterable, but5is not- A string like
"123"is iterable, but the integer123is not - A dictionary is iterable, but by default it loops over keys
Example:
for ch in "123":
print(ch)
This works because a string can be iterated over one character at a time.
Common causes
Here are the most common reasons for this error:
- Looping over an integer with
for - Using membership testing with an integer
- Passing an
intintolist(),sum(),any(),all(), or similar functions incorrectly - Reusing a variable name so a list becomes an integer
- Converting user input to
intbefore code that expected a string or list
Example of variable reuse causing the problem:
items = ["a", "b", "c"]
items = 3
for item in items:
print(item)
The variable items started as a list, but later it was changed to an integer.
FAQ
What does iterable mean in Python?
An iterable is an object you can loop over, such as a list, tuple, string, dictionary, set, or range.
Why does range(5) work but 5 does not?
range(5) creates an iterable sequence of numbers. The integer 5 is only one value.
Can I iterate over the digits of an integer?
Yes. Convert it first, such as str(123), if you want to loop over each digit as a character.
How do I loop 10 times in Python?
Use:
for i in range(10):
print(i)
not:
for i in 10:
print(i)
See also
- Python for loops explained
- Python
range()function explained - Python
type()function explained - How to convert int to string in Python
- TypeError:
'function' object is not iterable(Fix) - TypeError:
'bool' object is not iterable(Fix) - TypeError:
'NoneType' object is not iterable(Fix)
Check the variable type first, then decide whether you should use range() or a real iterable. That usually leads you to the correct fix quickly.