Fix Python Int Object Is Not Iterable Error

Haider Ali Oct 10, 2023
Fix Python Int Object Is Not Iterable Error

This error itself is self-explanatory. 'int' object is not iterable, it’s clearly saying that you can’t run iteration on an integer. An integer is a single digit, not a list that is iterable. Let’s take a look at some examples.

Fix 'int' object is not iterable Error in Python

Anything that returns or stores an integer is not iterable. That’s common knowledge. If you are not familiar with it, let’s understand iteration in python. Iterating can be done over a list, not on an integer. For instance, you can’t run a loop for iteration on an integer; it just doesn’t make sense. Take a look at the following code.

# integer
number = 123

# loop over an integer
for i in number:

Running the above code will give you the same error you are trying to avoid. In the above code, number is an integer having a single value 123. You can’t run a loop over it. If you are confused about the data type and its relevant functionalities, you can easily resolve it by finding the magic methods. In this case, we’ll use an integer. Take a look.

# integer
number = 123
# built-in / magic  methods of an integer

The output of the above code will be as follows.


As you can see, you can’t find the iterator method in the above list. Let’s see what’s the difference when it comes to a list.

# list
lst = [1, 2, 3]
# loop over a list
for j in lst:
# built-in /magic methods of a list

The above code will not give any error. You can iterate over a list. If you run the above code, you will also notice the _iter_ function in it, stating that you can use iteration on a list.

Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.


Related Article - Python Error