TypeError: argument of type ‘float’ is not iterable in Python

TypeError: argument of type 'float' is not iterable (Python)

TypeError: argument of type ‘float’ is not iterable in Python is a fairly common error when we want to compare two values of type Float using the membership operators. Let’s read this article to know how to fix it now.

When does the TypeError: argument of type ‘float’ is not iterable happen?

Python has the in operator to check if a value is a member of a sequence and the not in operator to check if a value is not a member. These operators are membership operators.

When we use membership operators with Float instead of an iterable object, we will get a TypeError. The following example will help you understand this error.

number1 = 3
number2 = 333.333

# Use the in with Float object
print(number1 in number2) # Error

Error:

TypeError: argument of type 'float' is not iterable

How to fix this error?

Here we will guide you through four ways to fix the above error.

Using an iterable object with the membership operator

Since membership operators require an iterable object, we must comply with that requirement. In our example, simply change the 2nd operand from Float to an iterable such as a List or Tuple. We will solve the problem by making this change.

number1 = 3
number2 = [333.333, 444.444]

# Use the in with a list
print(number1 in number2) # False
print(number1 not in number2) # True

Output:

False
True

Using comparison operator

If you want to compare the Integer value with the Float value, use the comparison operator instead of the membership operator. Like this:

number1 = 3
number2 = 3.0

# Use the comparison operator when comparing
print(number1 == number2) # True
print(number1 != number2) # False

Output:

True
False

Using isinstance() function

We mentioned the syntax of isinstance() in a previous post. You can read more here.

At this time, we’ll use the isinstance() function to check whether a variable is Float. And we’ll perform the membership operator only if the variable isn’t Float.

number1 = 3
number2 = 333.333

# Use isinstance() to check if a variable is a Float
if isinstance(number2, float):
    print('The membership operator cannot be used with Float')
else:
    print(number1 in number2)

Output:

The membership operator cannot be used with Float

Using type() function

The type() function in Python is a built-in function that allows you to check the data type of a variable.

Syntax:

type(value)

Description:

The type() function returns the data type of the value, such as “int”, “float”, or “boolean”.

Here, we will use the type() function to see if an object is iterable. If so, we can use in or not in operator. Otherwise, print a message line. Like this:

number1 = 3
number2 = 3.0
number3 = [3.0, 4.0, 5.0]

def isIn(first, second):
    # Use type() to determine if the second is an iterable
    if type(second) in (list, tuple, dict, str):
        print(first in second)
    else:
        print(f'{second} is not an iterable')

isIn(number1, number2) # 3.0 is not an iterable
isIn(number1, number3) # True

Output:

3.0 is not an iterable
True

Summary

The TypeError: argument of type ‘float’ is not iterable in Python and does not happen only with floats. It can happen with any data type that isn’t iterable, like booleans or integers. We hope this article helped you to handle errors like this easily. If you encounter any problems while programming in Python, visit LearnShareIT to find a solution.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *