How to Fix TypeError: ‘int’ Object Is Not Subscriptable In Python?

TypeError: 'int' object is not subscriptable in Python

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

Output:

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?

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:

Output:

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

Output:

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

Output:

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.

Output:

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.

Output:

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! 📚

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *