Here in this Python tutorial, we will be discussing one of the most common type errors in Python , “' int ' object is not iterable. ” It is a Python typeeror. By the end of this tutorial, you will have a complete idea about this common Python typeerror and how to debug it. As a Python programmer, you will be encountering many errors. Thus, debugging is one of the essential skills that every Python developer should have. You can not write an extensive Python program without committing any errors.
Even professional developers also commit many errors while writing Python code. Thus, exceptional error reading and debugging skills are what separates a great Python developer from the rest.
What is the ‘int’ object is not Iterable Python Typerror?
The error statement
typeerror: ‘int’ object is not iterable
is defining two messages
typeerror
and
int object is not iterable
.
Typeerror in Python
Typeerror
is an error that generally occurs in Python when you try to call a function or use an operator of an incorrect data type. For instance, if you perform an arithmetic operation between an integer and a string, then you will receive the Python typeerror. This is so because the arithmetic operators only operate between numeric values. For example:
2 + "three"
'int' object is not iterable
“ int object is not iterable ” is an error message that tells us that we are trying to iterate over an integer object. Integer objects in Python are not iterable , and that’s why Python throws this error. For example:
for i in 20:
print(i)
The Solution to “type:error int object is not iterable”
Now you know what is
typeerror
and “
int object is not iterable
,” let's proceed further. This error is one of the most common errors you will be encountering while programming in Python. In most cases, this error occurs because you forget to put
the
range()
function
inside the
for
loop.
Error Scenario Example
price_list = [20.30, 21.12, 23.78, 31.67, 77.89, 56.92, 74.92, 55.45, 66.78]
tax_list = []
for index in len(price_list):
tax_list.append(price_list[index] + (price_list[index] *0.25))
print(tax_list)
Output
for index in len(price_list):
TypeError: 'int' object is not iterable
In the above example, the for loop is trying to iterate over
len(price_list)
, which has an integer value of 9. The above error
“'int' object is not iterable”
can be solved by wrapping the
len(price_list)
function with the
range()
function. This is shown below:
Solution
price_list = [20.30, 21.12, 23.78, 31.67, 77.89, 56.92, 74.92, 55.45, 66.78]
tax_list = []
for index in range(len(price_list)):
tax_list.append(price_list[index] + (price_list[index] *0.25))
print(tax_list)
Output
[25.375, 26.400000000000002, 29.725, 39.587500000000006, 97.3625, 71.15, 93.65, 69.3125, 83.475]
Conclusion
Typeerror
is one of the most common errors in Python programs, and with every
typeerror
, you will also receive an error message that will tell you what exactly is wrong with your Python program. By reading the error message, you will get a brief idea of your error.
Python also provides the line number where the error has occurred. If you are encountering the
"'int' object is not iterable” error message
, this probably means that there is something wrong with your
for
loop, and you have forgotten to put the
range()
method. So do it, and it will be fixed.
People are also reading:
- Extract YouTube Comments in Python
- Install Python package using Jupyter Notebook
- Python Counter in Collections
- COPY File and Directory Using shutil in Python
- Assembly, Disassembly, and Emulation using Python
- Detect Contours in Images in Python
- Python Developer Salary
- 10 Top Essential Python Tricks & Tips
- Python Matrix
- Arrays in Python
Leave a Comment on this Post