Sometimes we get Typeerror float object is not iterable error that occurs when we tried to iterate a floating number. In Python, we can only iterate through iterable ( lists, dictionaries, strings). If we tried to iterate through a non-iterable object in Python, then we will get this error. In this short article, we will learn how we can solve Typeerror float object is not iterable error using various methods. Moreover, we will also discuss and try to understand the error by taking Typeerror float object is not iterable as an example.
Solve Typeerror float object is not iterable in Python
The error clearly says that we are performing an operation on the wrong data type which is why it says in the beginning that the error is from the TypeError category. After the colons, it gives the explanation of the error which says we are trying to iterate through a floating number which is not possible: The Typeerror float object is not iterable error occurs because we are trying to iterate through the floating object which is not iterable.
For example, consider the following script which gives the same error
# defining floating number
num = 4.3
# using for loop
for i in num:
print(i)
Output:

Now let us see how to solve the Typeerror float object is not iterable error:
Solution-1: Remove the floating from iterable
As we know we are getting errors because we are trying to iterate through the floating number which is not possible. So, the step is to identify the problem and remove the floating points from the iteration and add any iterable instead:
Solution-2: Using range() method
If the floating point is not put mistakenly, then most probably you are trying to find the range of values from the given number.
In such a case, it is recommended to use the range function: The range() function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a specified number.
Let us use the range() method which will solve the Typeerror float object is not iterable error.
# defining floating number
num = 4.3
# using for loop
for i in range(int(num)):
print(i)
Output:
0
1
2
3
As you can see, this time we were able to solve the error and iterate through a range:
Also, note that we have first converted the floating point into an integer value otherwise you will again get the error:
Solution-3: Using typecasting to solve the error
Another method to solve the error is using the typecasting method. Type Casting is the method to convert the variable data type into a certain data type in order to the operation required to be performed by users. We can convert the floating values to iterables to that e can iterate through them.
For example, now we will convert the float data type to a string
# defining floating number
num = 4.3
# using for loop with typecasting
for i in str(num):
print(i)
Output:
4
.
3
As you can see, we were able to get rid of the error:
Solution-4: Using Try-except block to handle the error
The try and except block in Python are used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program’s response to any exceptions in the preceding try clause.
Let us use the try-except block to handle the error.
# try block
try:
num = 4.5
# using for loop-- returns error
for i in num:
print(i)
# exccpt blck
except TypeError:
# customize message
print("Couldn't iterate!!")
Output:
Couldn't iterate!!
As shown above, the except block has handled the error and executed the statement without crashing the program itself.
Solution-5: Other possibilities
There can be other reasons for this issue as well. For example, when we tried to pass the floating number to some built-in methods which create iterables, we also get Typeerror float object is not iterable error.
For example, the following code also gives the same error when we tried to convert a floating number into a list or set.
# floating number
num = 2.32
# converting to list
list(num)
Output:

So, if you want to create a list of float numbers then there can be two possibilities.
First, you can either use the square brackets around the floating number and pass to list() function as shown below.
# floating number
num = 2.32
# converting to list
list([num])
Or you can also use the append method to create a list:
# floating number
num = 2.32
# emtpy list
l = []
# appending in list
l.append(num)
This will also solve the issue:
Understanding Typeerror float object is not iterable error
If you want to know what the Typeerror float object is not iterable error means. Then you can read this section as well:
Whenever you get an error in Python, it gives you a lot of information about the error: There are basically two parts to the error:
The first part shows the category of the error that we are getting (in our case the Typeerror) which is followed by a colon and then it provided the explanation of the error( float object is not iterable).
What is TypeError in Python?
The Python TypeError is an exception that occurs when the data type of an object in an operation is inappropriate. This can happen when an operation is performed on an object of an incorrect type, or it is not supported for the object
What is a float in Python?
Float is a function or reusable code in the Python programming language that converts values into floating point numbers. Floating point numbers are decimal values or fractional numbers like 133.5, 2897.11, and 3571.213, whereas real numbers like 56, 2, and 33 are called integers
You can use the type() method in Python to get the data type of the variable as shown below:
# variables
var1 = 4.23
# printing the type
print(type(var1))
Output:
<class 'float'>
Notice that we got the data type of the variable using the type() method.
What are iterable in Python?
An Iterable is an object which can be looped over or iterated over with the help of a for loop. Objects like lists, tuples, sets, dictionaries, strings, etc. are called iterable. In short and simpler terms, iterable is anything that you can loop over
Now, we understood the error, which is type error and it says that we are doing something wrong with the data types. Our mistake is that we are trying to iterate through floating numbers when in reality the floating numbers in Python are non-iterable.
Reasons for getting Typeerror float object is not iterable error
The TypeError float object is not iterable error occurs when you try to iterate over a float value, which is not possible as floats are not iterable. Here are some possible reasons for getting this error:
- Using a float value instead of a list or tuple in a for loop.
- Trying to unpack a float value using the unpacking operator (*) instead of a list or tuple.
- Calling a method that expects an iterable argument, but passing a float value instead.
- Passing a float value as an argument to a function that expects an iterable argument.
- Storing a float value in a variable that is used in a later loop as an iterable.
Summary
In this short article, we learned how we can solve Typeerror float object is not iterable. We discussed various reasons for the occurrence of these errors. Moreover, we also discussed how we can actually understand the errors in Python.
Other Articles
- TypeError: not all arguments converted during string formatting [Solved]
- ModuleNotFoundError: No module named ‘cv2’ in Python Solved
- [5 methods] Zipfile.badzipfile: file is not a zip file
- [Solved] AttributeError: ‘list’ object has no attribute ‘split’
- ImportError: numba needs numpy 1.21 or less-Solved