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:


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.



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


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



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.


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.



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


You may also like:

Related Posts

  • Convert dict to String in Python
    17 December

    Convert dict to string in python

    Table of ContentsHow to convert dict to string in python?Using str() method.Using the pickle module.Using the for loop.Using the json.dumps() method. In this post, we will see how to convert dict to String in Python. Python contains several data structures, and more often than not, there is a need for the conversion of one type […]

  • Python range float
    17 December

    Generate float range in Python [9 ways]

    Table of ContentsWhat is a floating-point number in Python?Why not use the simple and popular range() function?Python range float : How to generate floating-point numbers in Python?Using the numpy.linspace() method.Using the numpy.arange() method.Using list comprehension.Using generator comprehension.Using yield generator.Using a user-defined function.Using the itertools module and its functions.Using the map() function along with a lambda […]

  • Python callback function
    13 December

    Callback function in Python

    Table of ContentsWhat is Callback Function in Python?Applications of Callback Functions in PythonConclusion In python, functions are first-class objects. In other words, we can pass a function to a different function as an input argument. We can return a function from another function, and we can assign a function to a variable. In this article, […]

  • ValueError: too many values to unpack
    10 December

    [Fixed] ValueError: too many values to unpack (expected 2)

    Table of ContentsWhat is unpacking in python?What is ValueError: too many values to unpack (expected 2)?How to resolve ValueError: too many values to unpack (expected 2)?Conclusion While programming in python, we can assign values to two or more variables in a single statement. While doing so, you might get the ValueError exception with the message […]

  • Python List of dictionaries
    09 December

    List of Dictionaries in Python

    Table of ContentsHow to Create a List of Dictionaries in Python?How to Access Elements of a List of Dictionaries?Search in a List of Dictionaries in PythonUpdate Values in a List of Dictionaries in PythonHow to Sort List of Dictionaries in Python?Conclusion We use both lists and dictionaries in our python programs. Lists are used to […]

  • Python if not
    08 December

    Python If with NOT Operator

    Table of ContentsNOT Operator and IF Statement in PythonApplications of the if statement with not operator in pythonConclusion In python, we use conditional statements to implement logic in our program. For this, we use boolean expressions with if statements. In this article, we will see how we can use the if statement with the not […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.