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?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.