TypeError: argument of type ‘int’ is not iterable

In Python, the error TypeError: argument of type ‘int’ is not iterable occurs when the membership operator (in, not in) is used to validate the membership of a value in non iteratable objects such as list, tuple, dictionary. The membership operator can be used to verify memberships in variables such as strings, numbers, tuples and dictionary.

The membership operator (in, not in) in Python is used to test whether or not the given value exists in another variable. The operator searches the value in an iterable object. The error “TypeError: argument of type ‘int’ is not iterable” is due to the search of value in a non-iterable object.

Through this article, we can see what this error is, how this error can be solved. This type error is due to missing of an non-iterable object in the membership operator. The error would be thrown as like below.

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


Different Variation of the error

In different contexts the “TypeError: argument of type ‘int’ is not iterable” error is thrown in various forms in python. The numerous variations of TypeError are given below.

TypeError: argument of type 'int' is not iterable
TypeError: argument of type 'float' is not iterable
TypeError: argument of type 'bool' is not iterable
TypeError: argument of type 'complex' is not iterable
TypeError: argument of type 'long' is not iterable
TypeError: argument of type 'NoneType' is not iterable
TypeError: argument of type 'type' is not iterable


Root Cause

The membership operator searches for a value in an iterable object such as list, tuple, dictionary. If the value presents in an iterable object, returns true, otherwise it returns false. If you are looking for a value in non-iterable object such as int, float, boolean, python can not identify the presents of the value. In this case the python interpreter will throw that error.



How to reproduce this issue

If you are looking for a value in non-iterable object such as int, float, boolean, the python interpreter will throw this error. The code below searches an integer value in another integer value. This error will be thrown, as an integer value is not an iterable entity.

x = 4
y = 4
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 'int' is not iterable


Solution 1

If the membership operator (in, not in) is used, the value present in an iterable object such as list, tuple, or dictionary should be checked. Verify that the program looks for iterable objects. In the membership operator, the wrong variable is used, or the wrong value is given in the variable.

x = 4
y = [4, 5]
print x in y

Output

True


Solution 2

If you want to check a value with another value, you should not use the Membership Operator. Instead, the logical operator is used to compare two values. The logical operator in python will compare two values. The code below shows how to use comparison of the value.

x = 4
y = 4
print x == y

Output

True


Solution 3

If the membership operator variable contains None value, the membership operator would not be able to iterate that value. Membership operator throws “TypeError: the ‘NoneType’ argument is not iterable” error. Either Null check must be performed or assigned with an iterable object as the default.

x = 4
y = None
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 'NoneType' is not iterable

Solution

x = 4
y = []
print x in y

Output

False


Solution 4

If the type of the value is used in the membership operator, python interpreter will throw “TypeError: type ‘type’ argument is not iterable” error. The value presents check can not be performed on the type of the object. It is because of error in typing. The types such as list, tuple, dict are to be modified to functions such as list(), tuple(), dict().

x = 4
y = list
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 'type' is not iterable

Solution

x = 4
y = list()
print x in y

Output

False



Related Articles

Leave a Reply

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