Int object is not iterable python: what is it and solution

We can consider encountering an error as a learning opportunity, not a problem. While developing in Python, you may have seen an error: Int object is not iterable python.

Typeerror int object is not iterable 12

What does this mean? How to slove it? Therefore, those are the questions we’re going to answer in this article. We will discuss what the typeerror: ‘int’ object is not iterable is, why it is raised, and how you can solve it.

The Problem: Int object is not iterable python

“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.

To clarify, the error: int object is not iterable python message tells us that you have tried to iterate over an object that is not iterable. Iterable objects are items whose values you can access using a “for loop”.

A Practice Scenario

One of the most common scenarios in which this error: int object is not iterable python 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.

So, Consider the following code snippet:

1 def count_occurrence(values, to_find):
2      number_of_occurrences = 0
3      for v in len(values):
4          if values[v] == to_find:
5                 number_of_occurrences += 1
6       return number_of_occurrences
7
8 values = [1, 2, 3, 3]
9 check_for_threes = count_occurrence(values, 3)
10
11 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. In addition, this function iterates over all the values in “values” and keeps a running total of all those equal to a particular number. So, this number is specified as a parameter called “to_find”.
In the 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 the code:

1 Traceback (most recent call last):
2     File "main.py", line 9, in <module>
3        check_for_threes = count_occurrence(values, 3)
4     File "main.py", line 3, in count_occurrence
5         for v in len(values):
6 TypeError: 'int' object is not iterable

An error has been raised. Now that we’ve replicated this error, we can solve it.

The Solution for Int object is not iterable python

The 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:

1 for v in len(values):

The problem needs to iterate over a number.

len(values) is equal to 4. That’s how many values are in the list .Try to iterate over a number? Nothing happens. This is because for loops only work with iterable objects.

So, to solve this problem: Int object is not iterable python, 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:

1 for v in range(len(values)):

As a result, 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 the code again with the range() statement. It returns:

1 2

This code has successfully found all the instances of 3 in the list. It has counted them all up, and then printed the total number of times 3 appears in the list to the console.

Conclusion

Above all, 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. And, An int object is not iterable python error is raised when you try to iterate over an integer value.

Therefore, to solve this error, make sure that you are iterating over an iterable rather than a number.

What’s Next?

Above are our recommended Python Projects for beginners. However, to find the best projects, you should consider your ability, interest as well as schedule. If you need any help with your Python, don’t hesitate to contact ArrowHitech  – one of the best outsourcing IT in Vietnam.

typeerror int object is not iterable

Having more than 14 years of experience, we can help you handle difficulties in not only coding, but also in eCommerce. What are you waiting for? Contact us here

Tags

Share