How to Solve Python TypeError: ‘int’ object is not subscriptable

by | Programming, Python, Tips

We raise the TypeError: ‘int’ object is not subscriptable when trying to treat an integer type value like an array. Subscriptable objects in Python contain or can contain other objects in order, for example, a list. Integers cannot contain other objects; they single whole numbers. Trying to do an operation unsuitable for a specific type will raise a TypeError.

In this tutorial, we will detail the TypeError: ‘int’ object is not subscriptable error with examples to show you how to solve the problem in your code.


What is TypeError

TypeError occurs when you try to operate on a value that does not support that operation.

This particular TypeError occurs when we try to do an operation supported by subscriptable objects like strings, lists, tuples, and dictionaries, on an integer, which is not a subscriptable object. Subscriptable objects implement the __getitem__() method, whereas integers do not implement the __getitem__() method.

Like integers, floating-point numbers are not subscriptable objects. If we try to index or slice a floating-point number like a list, for example, to get the first digit, we will raise the error “TypeError: ‘float’ object is not subscriptable“.

Another example of a TypeError is concentrating a string with an integer using the ‘+’ operator. We go into more detail on this TypeError in the article titled “Python TypeError: can only concatenate str (not “int”) to str Solution“.

Methods also do not implement the __getitem__() method, and cannot be accessed like a list with square brackets. If you try to access a method like a list you will raise the error “TypeError: ‘method’ object is not subscriptable“.

Let’s look at an example of accessing an item in a list using indexing, remembering that the index of an array starts with 0:

# Define fruit basket list

fruit_basket = ["apple", "orange", "pineapple"]

#print output

print(fruit_basket[2])
pineapple

The code returns pineapple, showing us that lists contain objects, and we can retrieve them using indexing. We cannot apply this indexing operation to a non-subscriptable value like an integer or a float.

TypeError: ‘int’ object is not subscriptable

Trying to Access the Index of an Integer Object

In this example, we’ll start by creating an integer object and try to perform indexing on it.

# an integer

number = 40

# print the 0th index of an integer

print(number[0])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
1 print(number[0])

TypeError: 'int' object is not subscriptable

The TypeError tells us that performing indexing or other subscriptable operations on an integer value is illegal in Python.

Moving from the simple case, the most common scenario where we encounter this error is when our program requests input from a user, converts the information into an integer and later tries to access the input data as a string type value.

Let’s look at an example of a program requesting a sort code and account number. The program uses slicing to separate the two values from the input and prints the two values.

# Sort code and Account number input

bank_details = int(input("Enter your sort code and account number: "))

# Get sort code using slicing

sort_code = bank_details[0:5]

# Get account number using slicing

account_number = [5:]

#print two separate values

print('Sort Code: ', sort_code)

print('Account number: ', account_number)
Enter your sort code and account number: 00000012345678

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
1 sort_code=bank_details[0:5]

TypeError: 'int' object is not subscriptable

The traceback informs us that the statement sort_code = bank_details[0:5] is the cause of the TypeError. We are trying to get the sort code using the illegal subscriptable operator [0:5] on the bank_details integer value.

Solution

To solve this problem, we need to remove the int() function from the input() method, which will give us the bank_details as a string object, which is subscriptable.

# Sort code and Account number input

bank_details = input("Enter your sort code and account number: ")

# Get sort code using slicing

sort_code=bank_details[0:6]

# Get account number using slicing

account_number=bank_details[6:]

# Print output

print('Sort code: ', sort_code)

print('Account number:  ', account_number)
Enter your sort code and account number: 00000012345678

Sort code:  000000

Account number:   12345678

We can successfully index the bank_details string and obtain the sort code and account number. To learn more about getting substrings from string objects, visit the article titled “How to Get a Substring From a String in Python“.

Summary

Congratulations on making it to the end of the tutorial. To summarize, we raise the TypeError: ‘int’ object is not subscriptable when trying to perform illegal subscriptable operations on an integer. We can only perform subscriptable operations on subscriptable objects, like a list or a dictionary.

When performing slicing or indexing, ensure the value you are trying to slice is not an integer value. If you do not need to perform a subscriptable operation, you can remove the indexing on the integer. In program design, ensure you use sensible and meaningful names for your variables. The variable names should describe the data they hold, which will reduce the likelihood of TypeErrors.

To learn more about Python specifically for data science and machine learning, go to the online courses page on Python.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨