Register Login

TypeError: 'float' object is not iterable

Updated Jul 31, 2020

In this article, we will learn about the error TypeError: 'float' object is not iterable. This error occurs when we try to iterate through a float value which we know is not iterable.

TypeError: 'float' object is not iterable

TypeError: 'float' object is not iterable

Let us understand it more with the help of an example.

Example 1:

for i in 3.4:
 print("this doesn't work")

Output:

File "none3.py", line 1, in <module>
for i in 3.4:
TypeError: 'float' object is not iterable

Explanation:

In the above example, we are trying to iterate through a 'for loop' using a float value. But the float is not iterable. Float objects are not iterable because of the absence of the __iter__ method. Which we have discussed about in the below example 2.

Thus the error TypeError: float object is not iterable occurs.  

Example 2:

list = [2,4,8,3]
for x in list:
 print(x)

Output:

2
4
8
3

Explanation:

In the above example, we are trying to print the list elements using the 'for loop'. since the list is iterable, thus we can use the for loop for iteration.

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(3.4)

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__magicmethod is present.

Code:

print(dir(3.4))

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']

__iter__ magic method is absent.

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 float is not an iterable object and a list is an iterable object.


×