Last modified: Apr 23, 2026 By Alexander Williams
Fix 'int' Object Not Subscriptable Error
Have you seen the error TypeError: 'int' object is not subscriptable in Python? This is a common mistake for beginners. It happens when you try to use square brackets on an integer. Integers are not sequences. You cannot index or slice them like strings or lists.
This article explains why this error occurs. It shows clear examples. You will learn how to fix it fast. We will also cover how to avoid it in the future.
What Does Subscriptable Mean in Python?
In Python, a subscriptable object is one that supports the __getitem__ method. This method allows you to access items using square brackets. Lists, strings, tuples, and dictionaries are subscriptable. Integers and floats are not.
When you write my_list[0], Python calls the __getitem__ method. An integer does not have this method. So Python raises a TypeError.
Common Causes of the Error
The error usually happens when you confuse an integer with a list or string. Here are the most common causes:
- Treating a number like a list.
- Using indexing on a variable that holds an integer.
- Forgetting to convert a string to an integer before indexing.
- Accidentally overwriting a list variable with an integer.
Example 1: Direct Indexing on an Integer
This is the simplest case. You try to index a number directly.
# This will cause the error
number = 42
print(number[0])
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(number[0])
TypeError: 'int' object is not subscriptable
Fix: You cannot index an integer. If you need the first digit, convert the integer to a string first.
# Correct approach
number = 42
number_str = str(number)
print(number_str[0]) # Output: 4
Example 2: Confusing a List with an Integer
Sometimes you think a variable is a list, but it is an integer. This often happens with function return values.
# Mistake: using len() returns an integer
my_list = [10, 20, 30]
length = len(my_list) # length is 3 (an integer)
print(length[0]) # Error: trying to index an integer
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(length[0])
TypeError: 'int' object is not subscriptable
Fix: Use the list itself, not its length. If you need the first element, use my_list[0].
# Correct approach
my_list = [10, 20, 30]
print(my_list[0]) # Output: 10
Example 3: Overwriting a List Variable
This is a sneaky bug. You assign a list to a variable. Later in the code, you accidentally assign an integer to the same variable.
# Mistake: overwriting the list
data = [1, 2, 3]
data = len(data) # Now data is an integer (3)
print(data[0]) # Error
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(data[0])
TypeError: 'int' object is not subscriptable
Fix: Use a different variable name for the length. Keep the original list intact.
# Correct approach
data = [1, 2, 3]
data_length = len(data)
print(data[0]) # Output: 1
How to Debug and Prevent This Error
When you see this error, check these things first:
- Look at the variable you are indexing. Use
print(type(variable))to check its type. - Make sure you did not overwrite your list or string with an integer.
- If you need to access digits of a number, convert it to a string with
str(). - Use meaningful variable names to avoid confusion. For example, use
user_listinstead ofdata.
For more help with Python errors, check our guide on Python TypeError: Causes and Fixes. It covers many common type errors.
Real-World Scenario: Processing User Input
Imagine you ask a user for a number. You want to get the first digit. The input is a string, but you convert it to an integer too early.
# Mistake: converting to int before indexing
user_input = input("Enter a number: ")
number = int(user_input)
first_digit = number[0] # Error: int is not subscriptable
print(first_digit)
Enter a number: 123
Traceback (most recent call last):
File "main.py", line 4, in <module>
first_digit = number[0]
TypeError: 'int' object is not subscriptable
Fix: Index the string before converting to an integer. Or convert after indexing.
# Correct approach
user_input = input("Enter a number: ")
first_digit_str = user_input[0] # Index the string
first_digit = int(first_digit_str)
print(first_digit) # Output: 1
When You Should Use Subscripting
Use subscripting only on sequences. These include:
- Strings:
"hello"[0]gives"h" - Lists:
[1, 2, 3][1]gives2 - Tuples:
(4, 5, 6)[2]gives6 - Dictionaries:
{"a": 1}["a"]gives1
Do not use subscripting on numbers, booleans, or None.
Conclusion
The TypeError: 'int' object is not subscriptable error is easy to fix. It means you tried to use square brackets on an integer. Remember that integers are not sequences. Check the type of your variable with type(). Always convert integers to strings before indexing if you need digits. Use clear variable names to avoid overwriting lists. For more debugging tips, read our full guide on Python TypeError: Causes and Fixes. Practice these examples to build good habits. Happy coding!