Table of Contents
◈ Introduction
In this article, we will be discussing certain type of error in Python. To be more specific we will be discussing the reason behind the occurrence of : TypeError: 'int' Object Is Not Subscriptable
in Python and the methods to overcome such errors.
Let’s have a look at an example which demonstrates the occurrence of such errors.
Example: Consider the following program:
1 2 3 4 5 |
num = int(input("Enter a 3 digit number? ")) sum_digit = num[0]+num[1]+num[2] print(sum_digit) |
Output:
1 2 3 4 5 6 |
Traceback (most recent call last): File "D:/PycharmProjects/pythonProject1/TypeError Not Subscriptable.py", line 7, in <module> sum_digit = num[0]+num[1]+num[2] TypeError: 'int' object is not subscriptable |
If you have come across a similar bug/error, then it must have been really frustrating! 😩
But it also brings us to a list of questions:
- What does object not Subscriptable mean?
- What is a
TypeError
? - What is
TypeError:'int' object is not subscriptable
? - How do I fix:
TypeError:'int' object is not subscriptable
?
Hence, without further delay let us discover the answers to our questions and then solve our problem.
➥ What Does Object Not Subscriptable Mean?
In simple terms, a subscriptable object in Python is an object that can contain other objects, i.e., the objects which are containers can be termed as subscriptable objects. Strings
, tuples
, lists
and dictionaries
are examples of subscriptable objects in Python.
➟ Why is Integer not a subscriptable Object?
Integers are whole numbers. They cannot contain other objects within them. Further, subscriptable objects implement the __getitem__()
method and integer objects do not implement the __getitem__()
method.
➥ What is a TypeError In Python?
A TypeError
is raised when a certain operation is applied to an object of an incorrect type. For example, if you try to add a string object and an integer object using the +
operator, then you will encounter a TypeError
because the +
operation is not allowed between the two objects that are f different types.
Example:
1 2 3 |
print('Java'+2+'Blog') |
Output:
1 2 3 4 |
TypeError: can only concatenate str (not "int") to str |
➥ What is: TypeError:’int’ object is not subscriptable?
- You will encounter
TypeError: object is not subscriptable
in Python when you try to use indexing on an object that is not subscriptable. - Since integer is not a subscriptable object, thus if you try to use indexing upon an integer object then Python will throw the following error:
TypeError:'int' object is not subscriptable
.
That brings us to our next questions:- What are some of the scenarios where we come across TypeError:'int' object is not subscriptable
and how can we fix it?
To answer the above questions, let us visualize the occurrence and solution to this kind of TypeError
with help of examples.
✨ Scenario 1: Trying To Access Index Of An Integer Object
We have already discussed the problem statement in the introduction section of this article where we tried to find the sum of all the digits of a three-digit number.
However, we got TypeError: object is not subscriptable
in our futile attempt to derive the sum. The reason was: we treated the integer object num
as a container object and tried to access it using its indices.
Now, it’s time to make amendments and make our program work! 😃
The Solution:
✯ Method 1: Convert Integer Object to a String Object
A simple solution to our problem is:
- accept the user input
num
as a string, - Each digit can now be accessed using their index now as they are strings. Typecast each digit string to an integer and calculate the sum.
1 2 3 4 5 |
num = input("Enter a 3 digit number? ") sum_digit = (int(num[0]) + int(num[1]) + int(num[2])) print(sum_digit) |
Output:
1 2 3 4 |
Enter a 3 digit number? 754 16 |
✯ Method 2: Overwrite the __getitem__ Method
Another approach to solving the non-subscriptable TypeError
is to redefine the __getitem__
method in the code itself as shown below:
1 2 3 4 5 6 7 8 9 10 |
class Subs: def __getitem__(self, item): return int(item) obj = Subs() num = input("Enter a 3 digit number? ") print(obj[num[0]] + obj[num[1]] + obj[num[2]]) |
Output:
1 2 3 4 |
Enter a 3 digit number? 546 15 |
Explanation:
In this case, we defined the __getitem__ method in our code and made it return each digit of the three-digit number in the form of an integer.
✨ Scenario 2: Treating an Integer as a List
Given below is another scenario which raises – TypeError:'int' object is not subscriptable
.
1 2 3 4 5 6 7 8 |
item = input("Enter item name : ") price = input("Enter item price : ") x = 0 int(x[price]) discounted_Price = x-3000 print ("Price of an ",item, "is Rs.",discounted_Price) |
Output:
1 2 3 4 5 6 7 8 9 10 |
Enter item name : iPhone 12 Enter item price : 79900 Traceback (most recent call last): File "D:/PycharmProjects/pythonProject1/TypeError Not Subscriptable.py", line 4, in <module> int(x[price]) TypeError: 'int' object is not subscriptable Process finished with exit code 1 |
In the above program, price
is an integer value, however we tried to use it as a list by using its index. Thus we got the error!
The Solution:
The solution is pretty straightforward in this case. You just have to avoid using the integer object as a container type object.
1 2 3 4 5 6 7 8 |
item = input("Enter item name : ") price = input("Enter item price : ") x = (int(price)) discounted_Price = x - 3000 print("Price of an ", item, "is Rs.", discounted_Price) |
Output:
1 2 3 4 5 6 7 |
Enter item name : iPhone 12 Enter item price : 79900 Price of an iPhone 12 is Rs. 76900 Process finished with exit code 0 |
Conclusion
We learned some key points about how to deal with TypeError:'int' object is not subscriptable
in Python. To avoid these errors in your code, remember:
Python raises TypeError: object is not subscriptable
if you use indexing, i.e., the square bracket notation with a non-subscriptable object . To fix it you can:
- wrap the non-subscriptable objects into a container data type as a string, list, tuple or dictionary, or,
- by removing the index call, or
- by defining the
__getitem__
method in your code.
I hope this article helped! Please subscribe and stay tuned for more articles in the future. Happy learning! 📚