[Solved] TypeError: ‘int’ Object is Not Iterable

So, you have encountered the exception, i.e., TypeError: ‘int’ object is not iterable. In the following article, we will discuss type errors, how they occur and how to resolve them. You can also check out our other articles on TypeError here.

What is a TypeError?

The TypeError occurs when you try to operate on a value that does not support that operation. Let’s see an example of the TypeError we are getting.

number_of_emp = int(input("Enter the number of employees:"))

for employee in number_of_emp:
	name = input("employee name:")
	dept = input("employee department:")
	sal = int(input("employee salary:"))
TypeError: 'int' object is not iterable
TypeError: ‘int’ object is not iterable

Why ‘int’ object is not iterable?

Before we understand why this error occurs, we need to have some basic understanding of terms like iterable, dunder method __iter__.

Iterable

Iterable are objects which generate an iterator. For instance, standard python iterable are list, tuple, string, and dictionaries.

All these data types are iterable. In other words, they can be iterated over using a for-loop. For instance, check this example.

from pprint import pprint

names = ['rohan','sunny','ramesh','suresh']
products = {
	'soap':40,
	'toothpaste':100,
	'oil':60,
	'shampoo':150,
	'hair-gel':200
}
two_tuple = (2,4,6,8,9,10,12,14,16,18,20)
string = "This is a random string."

for name in names:
	print(name,end=" ")

print("\n")

for item,price in products.items():
	pprint(f"Item:{item}, Price:{price}")

print("\n")

for num in two_tuple:
	print(num,end=" ")

print("\n")

for word in string:
	print(word,end=" ")
Outputs of different iterables
Outputs of different iterables

__iter__ dunder method

__iter__ dunder method is a part of every data type that is iterable. In other words, for any data type, iterable only if __iter__ method is contained by them. Let’s understand this is using some examples.

For iterable like list, dictionary, string, tuple:

You can check whether __iter__ dunder method is present or not using the dir method. In the output image below, you can notice the __iter__ method highlighted.

print(dir(list))
__iter__ method present in list data type
__iter__ method present in list data type

Similarly, if we try this on int datatype, you can observe that __iter__ dunder method is not a part of int datatype. This is the reason for the raised exception.

print(dir(int))
__iter__ method not a part of int datatype
__iter__ method not a part of int datatype

How to resolve this error?

Solution 1:

Instead of trying to iterate over an int type, use the range method for iterating, for instance.

number_of_emp = int(input("Enter the number of employees:"))

for employee in range(number_of_emp):
	name = input("employee name:")
	dept = input("employee department:")
	sal = int(input("employee salary:"))
TyperError resolved using the range method
TypeError resolved using the range method.

Solution 2:

You can also try using a list datatype instead of an integer data type. Try to understand your requirement and make the necessary changes.

random.choices ‘int’ object is not iterable

This error might occur if you are trying to iterate over the random module’s choice method. The choice method returns values one at a time. Let’s see an example.

import random

num_list = [1,2,3,4,5,6,7,8,9]

for i in random.choice(num_list):
	print(i)
random.choices ‘int’ object is not iterable
random.choices ‘int’ object is not iterable

Instead, you should try something like this, for instance, using a for loop for the length of the list.

import random

num_list = [1,2,3,4,5,6,7,8,9]

for i in range(len(num_list)):
 	print(random.choice(num_list))
Iterating over random.choice
Iterating over random.choice

jinja2 typeerror ‘int’ object is not iterable

Like the above examples, if you try to iterate over an integer value in the jinja template, you will likely face an error. Let’s look at an example.

{% for i in 11 %}
  <p>{{ i }}</p>
{% endfor %}

The above code tries to iterate over integer 11. As we explained earlier, integers aren’t iterable. So, to avoid this error, use an iterable or range function accordingly.

FAQs on TypeError: ‘int’ object is not iterable

Can we make integers iterable?

Although integers aren’t iterable but using the range function, we can get a range of values till the number supplied. For instance:
num = 10
print(list(range(num)))

Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

How to solve self._args = tuple(args) TypeError ‘int’ object is not iterable problem?

This is a condition when the function accepts arguments as a parameter. For example, multiprocessing Process Process(target=main, args=p_number) here, the args accepts an iterable tuple. But if we pass an integer, it’ll throw an error.

How to solve TypeError’ int’ object is not iterable py2exe?

py2exe error was an old bug where many users failed to compile their python applications. This is almost non-existent today.

Conclusion

In this article, we tried to shed some light on the standard type error for beginners, i.e. ‘int’ object is not iterable. We also understood why it happens and what its solutions are.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments