typeerror: argument of type ‘float’ is not iterable

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:

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

Can I iterate over a float value in Python?

No, floats are not iterable in Python. You can convert the float to an iterable object or use a different type of variable.

Are there other types of variables that are not iterable in Python?

Yes, some other examples include integers, booleans, and NoneType.