Do you encounter “Typeerror argument of type int is not iterable” in your Python code?
And you don’t know how to fix it?
Then, this article is for you.
Through this article, we will know what the Typeerror argument of type int is not iterable means, how and why this error occurs and we will provide solutions to resolve this error.
So first, let us know what this error means.
What is the Typeerror argument of type int is not iterable?
The “Typeerror argument of type int is not iterable” is a specific error message that occurs in Python when you try to use the membership test operators in and not in with an integer value.
The in and not in operators are used to check if a value is present in an iterable object, such as a string, list, tuple, or set.
When you use these operators with an integer value, Python raises a TypeError.
Because integers are not iterable objects and cannot be searched for individual elements.
Iterable objects
In Python, an iterable object is any object that can be looped over, meaning that its elements can be accessed one by one.
Common examples of iterable objects include lists, tuples, dictionaries, strings, sets, and file objects.
Note: Not all objects in Python are iterable. For example, integers, floats, and booleans are not iterable objects.
Now let us discuss How and why the Typeerror argument of type int is not iterable error occurs.
How does the Typeerror argument of type int is not iterable occurs?
The “Typeerror argument of type int is not iterable” in Python occurs when you try to use an integer value as an iterable object.
Since an integer is not an iterable object, Python raises a TypeError with the message:
TypeError: argument of type 'int' is not iterable
Here is an example code that triggers this error:
String_value = 12345
print('my string' in String_value)
In this example code, the variable String_value is assigned the integer value of 12345.
The membership test operator (in operator) is then used to check if the string ‘my string’ is present in String_value.
However, since String_value is an integer and not an iterable object like a string or a list.
Python raises a TypeError with the message:
TypeError: argument of type 'int' is not iterable
Membership Test Operator
A membership test operator is a Python operator that checks whether a value is a member of a collection, such as a string, list, tuple, or set.
There are two membership test operators in Python:
- in Operator:
This operator returns True if the value is a member of the collection and False otherwise.
- not in Operator
This operator returns True if the value is not a member of the collection and False otherwise.
Now let us know how to fix the Typeerror argument of type int is not iterable.
Typeerror argument of type int is not iterable – Solutions
Here are the different solutions that you can use to fix this error:
Solution 1: Convert the integer to a string or use a list:
Convert the integer to a string using the str() function or use a list containing the integer value
Both of these data types are iterable and can be used with membership test operators.
For example:
String_value = 12345
print('1' in str(String_value))
In this example, we convert an integer value to a string using the str() function.
Then, we check if the character “1” is present in the resulting string using the in membership test operator.
The output of the code will be True because the character “1” is present in the string “12345”.
Solution 2: use the isinstance() function:
By using the isinstance() function if check if the variable is of the expected type.
If not, we can handle the situation by assigning a default value to the variable.
This way, we can ensure that the variable is always iterable before performing any operations on it.
For example:
String_value = 12345
if not isinstance(String_value, list):
String_value = []
print('a' in String_value) # Output False
In this example, we first check if String_value is not an instance of the list class using the isinstance() function.
If it is not a list, we assign an empty list [] to String_value.
This ensures that String_value is always iterable, even if it was previously assigned an integer value.
This approach can be used with any other iterable object such as strings, dictionaries, tuples and etc.
String_value = 12345
if not isinstance(String_value, str):
String_value = ""
print('a' in String_value) # Output False
In this, we are checking if String_value is not an instance of the str class using the isinstance() function.
Solution 3: Check if the value is not an integer before using in operator:
One way to avoid the Typeerror argument of type int is not iterable error when using the in operator is to check if the value is an integer before using it.
My_value = 12345
if not isinstance(My_value , int):
print('1' in My_value )
else:
print("My_value is not an integer") #output My_value is not an integer
In the example above, we first check if My_value is not an integer using the isinstance() function.
If it’s not an integer, we print a message indicating that My_value is not an integer.
If it is an integer, we don’t use the membership test operator directly on it to avoid the “Typeerror argument of type int is not iterable” error.
Instead, we can either convert it to a string or use a list, as we discussed earlier.
However, it’s safer to check if the value is of the expected type before using the membership test operator on it.
So those are the alternative solutions that you can use to fix “Typeerror argument of type int is not iterable”.
Hoping that all of them help you resolve your problem regarding the error.
Here are the other fixed Python errors that you can visit, you might encounter them in the future.
- typeerror unsupported operand type s for str and int
- typeerror: object of type int64 is not json serializable
- typeerror: bad operand type for unary -: str
Conclusion
In conclusion, in this article, we discussed the “Typeerror argument of type int is not iterable”, provided its causes, and give solutions that resolve the error.
By following the given solution, surely you can fix the error quickly and proceed to your coding project again.
I hope this article helps you to solve your problem regarding a Typeerror stating “argument of type int is not iterable”.
We’re happy to help you.
Happy coding! Have a Good day and God bless.