One of the common errors that you have may encounter while running a program in Python is: TypeError: argument of type ‘float’ is not iterable
In this tutorial, we will show you the solutions on how to solve the python typeerror: argument of type ‘float’ is not iterable.
Why the error argument of type ‘float’ is not iterable occur?
The typeerror argument of type float is not iterable typically occurs because when you are using the membership test operators value (in and not in) with a float value.
For example:
my_float = 3.14
print('x' in my_float)The code starts by assigning the value of 3.14 to a variable called “my_float“.
The value assigned is a floating point number, which means it has a decimal point.
The next line of code prints out a Boolean value, which is the result of the expression ‘x’ in my_float.
This expression checks whether the character ‘x‘ is present in the floating point number assigned to “my_float“.
Since ‘x‘ is not present in the number 3.14, the expression evaluates to False.
Therefore, if you run the example code the output of the code will be “error“.
C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 4, in
print(‘x’ in my_float)
TypeError: argument of type ‘float’ is not iterable
How to solve the error?
There are three solutions to solve this error.
Solution 1: Convert the float to a string
The first solution to solve the error is to convert the float to a string.
Let’s take a look at the example given below, on how to properly convert a float to a string in Python:
my_float = 3.14
my_string = str(my_float)
print(my_string)In this example code, we first assign the value of 3.14 to the variable “my_float“.
Next, we convert the floating point number to a string using the str() function.
Then, it will store the result in a new variable called “my_string“.
Finally, we print the value of “my_string” to the console or terminal using the print() function.
The output of the code will be the string representation of the float value is:
C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
3.14
Note: That the Strings are iterable, through converting the floating-point number to a string this will solves the error.
Solution 2: Using a Tracking where the variable got assigned a float
The second solution to solve the error is to track the location where the variable was assigned a float value and correct the assignment accordingly.
For example:
my_var = 3.14
if not isinstance(my_var, list):
my_var = []
print('x' in my_var)Here’s an explanation of the code:
This code initializes a variable called my_var with a value of 3.14.
Next, it checks whether my_var is not an instance of a list using the isinstance() function.
Since my_var is not a list, the condition is true, and the code block inside the if statement is executed.
Inside the if statement, my_var is assigned an empty list [].
Finally, the code checks if the character ‘x‘ is present in my_var using the in operator.
Since my_var is an empty list, the condition is false.
If you run the code the output will be false:
C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
False
Solution 3: Check if the value is not a float
The last solution to solve this error is to check if the value is not a float before using the in or not in operators.
For example:
my_var = 3.14
if not isinstance(my_var, float):
print('x' in my_var)
else:
print('value is a float')This example code, will check whether my_var is a float or not using the isinstance() function.
If my_var is not a float, the code checks if the character ‘x’ is present in my_var using the membership operator in.
If my_var is a float, the code prints the message ‘value is a float’.
C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
value is a float
Additional Resources
The following articles explain on how to solve the other common errors in Python:
- Typeerror: numpy.float64 object is not iterable [SOLVED]
- Typeerror: webelement object is not iterable
Frequently Asked Questions
What is Python TypeError and what causes it?
TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.
How do I quickly debug a Python TypeError?
Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.
Should I catch TypeError or let it propagate?
For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.
How do I prevent TypeError in production?
Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.
Where can I find more TypeError fixes?
Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.
Conclusion
To conclude, by following the solutions in this article it will be able to help you to resolve the error typeerror: argument of type ‘float’ is not iterable.
FAQs
No, floats are not iterable in Python. You can convert the float to an iterable object or use a different type of variable.
Yes, some other examples include integers, booleans, and NoneType.
