As a Python programmer, getting an error is part of the coding process. One of the common errors you might encounter is the “typeerror: int object is not iterable” error.
In this article, we will explain in detail what this error means, why it occurs, and how to fix it.
Why the typeerror: int object is not iterable error occur?
The python typeerror: int object is not iterable error typically occurs because you are trying to iterate over an integer object using a loop, such as a for loop or a while loop.
However, integers in Python are not iterable, in other words, you cannot loop over them directly.
If you’re trying to iterate over an integer using a loop, Python will raise the “TypeError” exception.
Here is an example of why the error occurs:
my_int = 1357
for digit in my_int:
print(digit)
The output will raise an error message:
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 2, in
for digit in my_int:
TypeError: ‘int’ object is not iterable
How to solve the typeerror ‘int’ object is not iterable error?
These are the solutions to solve the error ‘int’ object is not iterable which are the following:
Solution 1: Using a range() function
Let’s take an example: Which is Iterating over an integer.
num = 10
for x in num:
print(i)
This example code will raise a typeerror: ‘int’ object is not iterable because num is an integer, which is not iterable.
The first way to solve this error int object is not iterable is to use a range() function.
For example:
num = 10
for x in range(num):
print(x)
This example codes defines a variable “num” as 10.
Then, it will print the numbers 0 to 9 (exclusive of 10) using a “for” loop and the “range” function.
Finally, when you the code the output will be sequence of numbers from 0 to 9:
C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
0
1
2
3
4
5
6
7
8
9
Solution 2: Convert the integer into a string
The second solution to solve this error is to convert the integer into a string.
Let’s take the example:
num = 10
for i in str(num):
print(i)
This example code defines a variable named “num” and assigns it a value of 10.
Next, it iterates over each character in the string representation of “num” using a “for” loop.
Then, Inside the loop, the current character is printed to the console using the “print” function.
In this particular case, since “num” is equal to 10, the loop will iterate over the characters “1” and “0” and print each one on a separate line.
If you run the code the output will be:
1
0
Solution 3: Pass an iterable object such as a list, tuple, or string
For example: Passing an integer to a function that expects an iterable
def sum_list(numbers):
total = 0
for num in numbers:
total += num
return total
sum_list(10)
This example code will raise a TypeError because sum_list function expects an iterable object but an integer is passed to it.
The last solution to solve this error is to Pass an iterable object such as a list, tuple, or string to the sum_list function:
For example in using a List() function:
def sum_list(iterable):
total = 0
for item in iterable:
total += item
return total
my_list = [1, 2, 3, 4, 5]
print(sum_list(my_list))
Output:
15
Example for a tuple:
def sum_list(iterable):
total = 0
for item in iterable:
total += item
return total
my_tuple = (6, 7, 8, 9, 10)
Output:
40
The example code defines a function sum_list that takes an iterable object as its argument and returns the sum of all the items in the iterable.
Then, the code creates a tuple called my_tuple containing the integers 6, 7, 8, 9, and 10.
The sum_list function does not call with any argument, but it can be called later by passing the my_tuple tuple (or any other iterable object) as an argument to obtain the sum of its items.
Additional Resources
If you want to learn more about Python errors and how to handle common errors like the typeerror.
Here are some additional resources that you may find helpful:
- Typeerror: ‘numpy.intc’ object is not iterable
- Typeerror: ‘map’ object is not subscriptable
- Typeerror: builtin_function_or_method object is not iterable
- typeerror: ‘int’ object is not callable
- Typeerror argument of type int is not iterable
Conclusion
The “TypeError: ‘int’ object is not iterable” error occurs if you are trying to use an integer in an iterable context.
It can be fixed by either Using a range() function, Converting the integer into a string and Passing an iterable object such as a list, tuple, or string.
FAQs
In Python, iteration is the process of looping over each item in a collection, such as a list or a tuple.
No, integers are not iterable in Python.
Some examples of iterables in Python include lists, tuples, strings, and dictionaries.
You can convert an integer to an iterable by converting it to a string and then iterating over the characters of the string.