Encountering an error is not a problem; it’s a learning opportunity. While developing in Python, you may have seen an error “‘int’ object is not iterable”.
What does this mean? How do I solve it? Those are the questions we’re going to answer in this article. We will discuss what the “‘int’ object is not iterable” error is, why it is raised, and how you can solve it.
The Problem: typeerror: ‘int’ object is not iterable
“typeerror: ‘int’ object is not iterable”
There are two parts to this error message: TypeError and the error message.
A TypeError is raised when a function is applied to an object of the wrong data type. For instance, if you try to apply a mathematical function to a string, or call a value like a function which is not a function, a TypeError is raised.
A Practice Scenario
One of the most common scenarios in which this error is raised is when you try to use a for loop with a number. This mistake is made because it’s easy to forget to use the range() function when you are using a for loop.
Consider the following code snippet:
def count_occurrence(values, to_find): number_of_occurrences = 0 for v in len(values): if values[v] == to_find: number_of_occurrences += 1 return number_of_occurrences values = [1, 2, 3, 3] check_for_threes = count_occurrence(values, 3) print(check_for_threes)
This code snippet uses one function. The count_occurance function counts how many times a number appears in the “values” list. This function iterates over all the values in “values” and keeps a running total of all those equal to a particular number. This number is specified as a parameter called “to_find”.
In our main program, we define a list called “values” with four values. We call our count_occurrence function to count how many threes are in our list of values. We then print out the response to the console.
Let’s run our code:
Traceback (most recent call last): File "main.py", line 9, in <module> check_for_threes = count_occurrence(values, 3) File "main.py", line 3, in count_occurrence for v in len(values): TypeError: 'int' object is not iterable
Oh no! An error has been raised. Now that we’ve replicated this error, we can solve it.
Our error tells us that we’ve tried to iterate over an object that is not iterable. If we look at the error message in detail, we can see it points us to the line where the problem occurs:
for v in len(values):
The problem with this line is that we are trying to iterate over a number.
len(values) is equal to 4. That’s how many values are in the list “values”. If we try to iterate over a number, nothing happens. This is because for loops only work with iterable objects.
To solve this problem, we need to make sure our for loop iterates over an iterable object. We can add a
range() statement to our code to do this:
for v in range(len(values)):
This statement will create an iterable object with a list of values in the range of 0 and the number of items in the “values” list.
Let’s try to run our code again with the
range() statement. Our code returns:
Our code has successfully found all the instances of 3 in the list. Our code has counted them all up and then printed the total number of times 3 appears in the list to the console.
TypeErrors are a common type of error in Python. They occur when you try to apply a function on a value of the wrong type. An “‘int’ object is not iterable” error is raised when you try to iterate over an integer value.
To solve this error, make sure that you are iterating over an iterable rather than a number.
Now you’re ready to solve this error like a Pythonista!