In Python, when an operation is performed on an incorrect or unsupported object, then the “TypeError” occurs in the python program. Some objects in Python are iterable such as list, tuple, set, dict, etc., while others are not, like integer, float, etc. The “TypeError” mostly occurs while performing iteration on a non-iterable object in Python. To resolve the error, various solutions are provided in this article.
This Python write-up will cover the below-listed reasons and solutions regarding the “TypeError: int object is not iterable” error in Python:
- Reason: Iterating Over an Integer
- Solution: Use range() Function
- How to Check Iterable Objects in Python?
So, let’s get started!
Reason: Iterating Over an Integer
The prominent reason which causes the “TypeError” in Python programs is trying to iterate over an integer. The stated error is demonstrated in the below snippet:
The above snippet shows a “TypeError” generated due to iterating over an integer.
Solution: Use range() Function
To resolve this error, the built-in “range()” function is used to iterate over the specific integer number. The “range()” function can accept three parameters, i.e., start, stop and step. In the below code, the “for” loop along with the range() function is used to iterate over the integer:
int_num = 10 for num in range(int_num): print(num)
In the above code, range() function accepts the integer variable as an argument. The for loop iterates over the “range()” function containing the integer value “10”.
Note: If a single value is passed inside the “range()” function, that value will be assigned to the stop parameter, and the default value assigned for the start (0) and step (1) parameters.
The above output shows the range for the specified integer value, i.e., (0-9).
Note: One other reason that causes this error in Python is passing integer values inside the iterable function such as list(), set(), tuple(), etc. To rectify such an error, the assignment must be corrected, and the integer value must be assigned to function according to the requirement.
How to Check Iterable Objects in Python?
To check whether the object is iterable, the dir() method is used in python programs. Input object properties and methods can be viewed using the “dir()” function. if the “__iter__” method is found in the object properties, it indicates that the given object is iterable. In the code example shown below, the “dir()” method is used to check iterable object:
Code: Using Integer Value
int_val = 32 print(dir(int_val))
In the above code, the “dir()” method accepts the integer variable and returns all the properties and methods.
The above output shows that the “__iter__” method does not exist in the object directories. As a result, the given object cannot be iterated.
Code: Using List
list_val =  print(dir(list_val))
In the above code, the list is created and passed inside the parentheses of the “dir()” method to check whether the list is an iterable object or not.
The above output verified that the “__iter__” method is present in the properties of the input list. It proves that a List is an iterable object.
That’s it from this guide!
The “TypeError: ‘int’ object is not iterable in Python” occurs when a user tries to iterate over an integer object or passes an integer inside the iterable function, such as list(), tuple(), etc. To resolve this error, use the “range()” function to iterate over the integer or correct the assignment while passing the integer value to a built-in iterable function. The “dir()” method accepts the input variable as an argument and retrieves all properties and methods of the input variable. Using appropriate coding examples, this article presented reasons and solutions for TypeError “int object is not callable” in Python.