The python error TypeError: argument of type ‘float’ is not iterable occurs when the membership operators check a value in a float variable. The python membership operator (in, not in) checks a value in an iterable objects such as tuple, list, dict, set etc. If the iterable object is passed as a float variable, then the error will be thrown.

The membership operator (in, not in) is used to verify whether the given value is present in an iterable object or not. If the value presents in the iterable object, then it returns true. Otherwise it returns false. If the iterable object is a float variable then it is difficult to check for the value. The error TypeError: argument of type ‘float’ is not iterable is due to a value check in a float variable.

If the correct iterable object such as tuple, list, dict, set etc is passed in the membership operator, then the error TypeError: argument of type ‘float’ is not iterable will be resolved.



Exception

The error TypeError: argument of type ‘float’ is not iterable will display the stack trace as below.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x in y
TypeError: argument of type 'float' is not iterable
[Finished in 0.1s with exit code 1]


How to reproduce this error

The membership operator requires an iterable object to check a value. If the iterable object is passed as a float variable, then the value is hard to verify. Therefore, the error TypeError: argument of type ‘float’ is not iterable will be thrown.

x = 4
y = 4.0
print x in y

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x in y
TypeError: argument of type 'float' is not iterable
[Finished in 0.1s with exit code 1]


Solution 1

If the membership operator (in, not in) requires an iterable object to check a value present or not. The iterable object such as list, tuple, or dictionary should be used to verify the value. If the membership operator is passed with a valid iterable object, then the error will be resolved.

x = 4
y = [4.0]
print x in y

Output

True
[Finished in 0.1s]


Solution 2

If you wish to compare a float value with another float value, you should not use the Membership Operator. The logical operator is often used to equate two float values. In python the logical operator can compare two values. The code below illustrates how comparison of the value can be used.

x = 4
y = 4.0
print x == y

Output

True
[Finished in 0.1s]


Solution 3

If the iterable object in the Membership Operator is an invalid float value, then you can change it to execute in alternate flow. This will fix the error.

In the example below, if the iterable object is an invalid float value, then it shows an error message. This will resolve the error.

x = 4
y = 4.0
if isinstance(y, float) : 
	print "Value is float"
else:
	print x == y

Output

Value is float
[Finished in 0.0s]


Solution 4

If the iterable object is unknown, the expected iterable object should be checked before the membership operator is invoked. If the object is an iterable object, the membership operator is called with the python value. Otherwise, take an alternate flow.

x = 4
y = 4.0
if type(y) in (list,tuple,dict, str): 
	print x in y
else:
	print "not a list"

Output

not a list
[Finished in 0.1s]


Solution 5

The try and except block is used to capture the unusual run time errors. If the python variable contains iterable object, then it will execute in the try block. If the python variable contains the unutterable value, then the except block will handle the error.

x = 4
y = 4.0
try : 
	print x in y
except :
	print "not a list"

Output

not a list
[Finished in 0.0s]



Leave a Reply