DEV Community

Cover image for Int Object is Not Iterable – Python Error [Solved]
professional writer
professional writer

Posted on

Int Object is Not Iterable – Python Error [Solved]

If you are running your Python code and you see the error “TypeError: 'int' object is not iterable”, it means you are trying to loop through an integer or other data type that loops cannot work on.

In Python, iterable data are lists, tuples, sets, dictionaries, and so on.

In addition, this error being a “TypeError” means you’re trying to perform an operation on an inappropriate data type. For example, adding a string with an integer.

Today is the last day you should get this error while running your Python code. Because in this article, I will not just show you how to fix it, I will also show you how to check for the iter magic methods so you can see if an object is iterable.

How to Fix Int Object is Not Iterable

If you are trying to loop through an integer, you will get this error:

count = 14

for i in count:
    print(i)
# Output: TypeError: 'int' object is not iterable
Enter fullscreen mode Exit fullscreen mode

One way to fix it is to pass the variable into the range() function.

In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.

The loop will now run:

count = 14

for i in range(count):
    print(i)

# Output: 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 10
# 11
# 12
# 13
Enter fullscreen mode Exit fullscreen mode

Another example that uses this solution is in the snippet below:

read more

Top comments (0)