typeerror float object is not iterable : Step by Step Solution

typeerror float object is not iterable Fix it

Float is an non iterable python datatype and typeerror float object is not iterable occurs only when any python statement invokes float as iterable element in loop etc. In this article, we will explore multiple scenarios where we face this error. We will understand the root cause of this error and apply the same to fix different scenarios.

Typeerror float object is not iterable ( Deep Dive in Root Cause ) –

Usually we run the loop over iterable objects. In each iteration , It returns next value for the sequence. Like list, dict, tuple are iterable object. But float is not iterable object. Its single value element. Lets see with code for more clarity-

element= 7.5
for i in element:
  print(i)
Typeerror float object is not iterable
Typeerror float object is not iterable

How to check float is iterable or not ?

If any python object is iterable then its override the method __iter__() in its class and the best way to check the same is using dir() function.

print(dir(float))

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

Since __iter__() is missing hence float is not iterable object

Typeerror float object is not iterable ( Solutions ) –

There are infinite business logic where we face this error. But developer intention and root cause for this error is common everywhere. In this section lets understand the same.

Case 1 : Solution for looping scenario using range –

Usually when we want to loop any logic, we need to define the counter and there if we pass float we get this error. The straight fix for this using range. Basically range converts number to an iterable list. But make sure range only accepts interger values so if we pass float directly into range function we get this error – Typeerror: float object cannot be interpreted as an integer . Hence we will convert the float to int and then pass in range() function. Here is the implementation-

element= 7.5
for i in range(int(element)):
  print(i)
float object is not iterable range()
float object is not iterable range()

 

Case 2 : Float iteration as str object –

As you know str objects in python are iterable. In some scenarios developer needs to iterate Float object as string only. But they somehow forgot to typecast the float object into str . Hence they get this error.

element= 7.5
for i in str(element):
  print(i)
float object is not iterable fix by str()
float object is not iterable fix by str()

Here interpreter is iterating over the digits of float object as string.

Similar Typeerror on Iterable objects –

Just like Float, int and NoneType objects are not iterables. These error are similar to each others but occurs in different business logics. You can read more about them here-

Typeerror nonetype object is not iterable : Complete Solution

Typeerror int object is not iterable : Root cause and Fix

 

Thanks 

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner