TypeError: ‘float’ object is not iterable in Python

Only iterable objects such as list, tuple, set, etc. can be iterated in Python. While iterating, these iterators return each element one by one, unlike non-iterable objects. The non-iterable object like float will return “TypeError: float object is not iterable” when the user tries to iterate over it or pass inside the built-in iterable function.

This write-up will provide various reasons and solutions for “TypeError: float object is not iterable” in Python. The following aspects will be covered in this blog post in detail:

So, let’s begin!

Reason 1: Iterating Over a Float

The reason that causes the “TypeError” is when the “for” loop or any other iterator iterates over the float value in Python.

The above snippet shows the “float object is not iterable” error in the program when we try to iterate over the float value using a for loop.

Solution 1: Use range() Function

To rectify this error, the inbuilt “range()” function can be used along with the “int()” function. The range() function will iterate over the range of float numbers, while the int() function will convert the float into an integer.

Note: the range() function accepts integer input that’s why the float value is converted into an integer.

Code:

float_value = 5.45

for item in range(int(float_value)):
    print(item)

In the above code, the “range()” function is used with the int() function to convert the float into an integer and calculate the range of the integer.

Output:

The above output shows the range of numbers.

Solution 2: Using try-except Block

The other way to handle the error “float object is not iterable” in Python is using the “try-except” exception handle block.

Code:

float_value = 5.6

try:
    for item in float_value:
        print(item)
       
except TypeError:
    print('Given object is not iterable')

In the above code, try-except is used to handle the TypeError. If the given object is iterable, like a list, set, or dictionary, then the “try” block executes. While the “except” block will execute if the given object is not-iterable.

Output:

The above output shows that the except block is executed because the given value/object is not iterable.

Reason 2: Passing a Float to an Inbuilt Function

Another prominent reason that causes this error in Python programs is by passing a float value to an inbuilt function such as list(), tuple(), dict(), etc.

The above snippet shows that the type error occurs when the tuple() and set() function takes the float value as an argument.

Solution: Correct the Assignment

To resolve this error, we must correct or change the float assignment according to function. The tuple() and set() functions are initialized by taking multiple arguments inside the parenthesis.

Code:

value = tuple([4.4, 2.4, 3.3])
print(value) 

value = set(['2.8', '3.2', '8.3'])
print(value)

In the above code, the float value is changed according to function and each assignment will be placed according to the syntax of the respective built-in function.

Output:

The above output shows the value of the tuple and set created by correcting the assignment.

That’s it from this guide!

Conclusion

The “TypeError: float object is not iterable” occurs when the float is passed inside the built-in iterable function, or the user tries to iterate over the float. To resolve this error, various approaches are utilized in Python, such as using the range() function, using try-except block, correcting the assignment, etc. the range() function does not directly accept float value as an argument, so firstly, the float value must be converted into integer and then pass the converted value to the range() function. This article presented a detailed guide on various reasons and solutions for the “float object is not iterable” error in Python.