The python error TypeError: argument of type ‘bool’ is not iterable happens when the membership operators evaluates a value with a boolean variable. The python membership operator (in, not in) tests a value in an iterable objects such as set, tuple, dict, list etc. If the iterable object in the membership operator is passed as a boolean variable then the error is thrown.

The membership operator shall detect a boolean value as an iterable object. The boolean value is a single object. The member operator returns true if the value is found in an iterable object. 

The Member Operator (in, not in) is used to check whether or not the given value is present in an iterable object. If the value exists in the iterable object then it returns true. If the iterable object is a boolean variable then the value is hard to verify. The error TypeError: argument of type ‘bool’ is not iterable is due to checking the value in Boolean variable.

If the right iterable object is passed in the membership operator, such as tuple, set, dictionary, list etc., then the error will not occur.



Exception

When the error TypeError: argument of type ‘bool’ is not iterable happens, this error displays the stack trace as shown below.

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


How to reproduce this error

To verify a value the membership operator needs an iterable object. If the iterable object is passed as a boolean variable then it is difficult to check the value. Therefore, the error TypeError: argument of type ‘bool’ is not iterable will be thrown.

In the example below, two variables are assigned to x and y with a boolean value. The x value is verified with the y value used by the membership operator (in, not in). Since the y value is a boolean type, it will throw an error.

x = True
y = True
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 'bool' is not iterable
[Finished in 0.1s with exit code 1]


Solution 1

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

x = True
y = [ True ]
print x in y

Output

True
[Finished in 0.1s]


Solution 2

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

x = True
y = True
print x == y

Output

True
[Finished in 0.1s]


Solution 3

If the iterable object in the Membership Operator is an invalid boolean value, then you can change it to execute in alternate flow. This will fix the error. The iterable object must be validated before proceed with the membership operator.

x = True
y = True
if isinstance(y, bool) : 
	print "Value is boolean"
else:
	print x == y

Output

Value is boolean
[Finished in 0.1s]


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 = True
y = True
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 uniterable value, then the except block will handle the error.

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

Output

not a list
[Finished in 0.1s]



Leave a Reply