While programming in Python, it is a common practice to use loops such as for loops and while loops. These are used for iterating over lists and dictionaries for performing a variety of operations on the elements. But programmers often encounter an error called TypeError: 'int' object is not iterable.
This type of error occurs when the code is trying to iterate over a list of integer elements.
Let us understand it more with the help of an example.
Example 1
# Initializing an integer variable
Var = 5
# Iterating through a loop
# Using an integer value
for i in Var:
print("This will raise an error")
Output
File "none3.py", line 6, in <module>
for i in var:
TypeError: 'int' object is not iterable
Explanation
In the above example, we are trying to iterate through a for loop using an integer value. But the integers are not iterable. As the Var variable holds a single integer value 5, it cannot be iterated using a for loop or any other loop.
This is because of the absence of __iter__ method. Which we have discussed about below in example 2.
Thus the error “TypeError: int object is not iterable” occurs.
Example 2
# Initializing the list
MyList = [2,4,8,3]
# Iterating through a List
for x in MyLlist:
print(x)
Output
2
4
8
3
Explanation
In the above example, we printing the elements of the list using the for loop. since the list is an iterable object, thus we can use the for loop to iterate through it. Thus, the TypeError is not encountered here. Dictionaries are also iterable in Python using the loops.
To know whether an object is iterable or not we can use the dir() method to check for the magic method __iter__ . If this magic method is present in the properties of specified objects then that item is said to be iterable
To check, do: dir(list) or dir(5)
Code
List= [ ]
print(dir(list))
Output
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
__iter__ magic method is present.
Code
# Initializing an integer variable
Var = 5
# Printing methods associated with integer
print(dir(Var))
Output
['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__set_format__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
On reviewing the output of the code. We notice that __iter__ magic method is absent. Thus, integers are not iterable.
Conclusion
The presence of the magic method __iter__ is what makes an object iterable. From the above article, we can conclude that. The __iter__ method is absent in the float object. Whereas it is present in the list object. Thus integer is not iterable object, unlike list.