How to Resolve "TypeError: 'Int' Object Is Not Subscriptable" in Python
The TypeError: 'int' object is not subscriptable is a common Python error that trips up beginners and experienced developers alike. It occurs when you try to use indexing (square bracket notation) on an integer , i.e. a data type that simply doesn't support that operation.
This guide explains why this error happens, walks through the most common scenarios that trigger it, and shows you how to fix each one with clear examples.
What Does 'Int Object Is Not Subscriptable' Mean?
In Python, subscriptable means an object supports indexing via square brackets ([]). Data types like lists, strings, tuples, and dictionaries are subscriptable because they contain sequences or mappings of elements.
Integers, however, represent a single whole number, they don't contain a collection of items, so using [] on them makes no sense to Python.
x = 42
print(x[0])
Output:
TypeError: 'int' object is not subscriptable
You're asking Python to retrieve the "first element" of 42, but 42 isn't a container: it's just a number.
Common Causes and How to Fix Them
1. Accidentally Indexing an Integer Variable
The most straightforward cause is attempting to use bracket notation on a variable that holds an integer value.
Wrong:
number = 42
first_digit = number[0]
# TypeError: 'int' object is not subscriptable
Fix: Convert the integer to a string first:
number = 42
number_str = str(number)
first_digit = number_str[0]
print(first_digit)
Output:
4
If you need the result as an integer, wrap it with int():
number = 42
first_digit = int(str(number)[0])
print(first_digit) # Output: 4
print(type(first_digit)) # Output: <class 'int'>
2. Function Returning an Integer Instead of a List
A subtle and common bug occurs when a function sometimes returns a list and sometimes returns an integer, depending on a condition. If you always treat the result as a list, you'll get this error on certain inputs.
Wrong:
def get_data(condition):
if condition:
return [1, 2, 3]
else:
return 42 # Returns an integer, not a list!
result = get_data(False)
first_element = result[0]
# TypeError: 'int' object is not subscriptable
Output:
TypeError: 'int' object is not subscriptable
Fix: Ensure consistent return types:
def get_data(condition):
if condition:
return [1, 2, 3]
else:
return [42] # Always return a list
result = get_data(False)
first_element = result[0]
print(first_element)
Output:
42
Functions should return the same type regardless of the code path. If a function is expected to return a list, it should always return a list, even if it contains a single element or is empty. This prevents TypeError surprises downstream.
3. Overwriting a List Variable with an Integer
This often happens in loops or when reusing variable names. A variable that originally held a list gets reassigned to an integer, and later code still tries to index it.
Wrong:
values = [10, 20, 30]
values = len(values) # Now `values` is the integer 3
print(values[0]) # TypeError!
Output:
TypeError: 'int' object is not subscriptable
Fix: Use a different variable name:
values = [10, 20, 30]
count = len(values) # Use a separate variable
print(values[0]) # Works fine
print(count) # Works fine
Output:
10
3
A very common version of this mistake is accidentally naming a variable the same as a built-in function or a previous list:
list = [1, 2, 3] # Shadows the built-in list() function
list = 5 # Now it's an integer
print(list[0]) # TypeError!
Avoid using names like list, dict, str, int, or sum as variable names: they shadow Python's built-in functions and lead to confusing errors.
4. Confusing Parentheses with Square Brackets
Sometimes the error stems from a simple typo: using square brackets [] when you meant to use parentheses () for a function call or arithmetic grouping.
Wrong:
result = 5
doubled = result[2] # Meant to multiply, not index!
Fix:
result = 5
doubled = result * 2
print(doubled)
Output:
10
5. Extracting Digits from an Integer
A frequent real-world need is accessing individual digits of an integer. Since integers aren't subscriptable, you need to convert them first.
Method 1: Convert to string:
number = 98765
digits = [int(d) for d in str(number)]
print(digits)
print(digits[0]) # First digit
print(digits[-1]) # Last digit
Output:
[9, 8, 7, 6, 5]
9
5
Method 2: Use arithmetic (no string conversion):
number = 98765
last_digit = number % 10
first_digit = number // 10000
print("First digit:", first_digit)
print("Last digit:", last_digit)
Output:
First digit: 9
Last digit: 5
Defensive Coding: Check the Type Before Indexing
When working with data from external sources, user input, or functions with mixed return types, it's wise to verify the type before attempting to index:
def safe_index(value, index):
if isinstance(value, (list, tuple, str)):
if index < len(value):
return value[index]
else:
return f"Index {index} is out of range"
else:
return f"Cannot index a {type(value).__name__} object"
print(safe_index([10, 20, 30], 0)) # Works: list
print(safe_index("hello", 1)) # Works: string
print(safe_index(42, 0)) # Handled gracefully
Output:
10
e
Cannot index a int object
Quick Summary of Fixes
| Cause | Fix |
|---|---|
| Indexing an integer directly | Convert to str or list first |
Function returns int instead of list | Ensure consistent return types |
| Variable overwritten from list to int | Use distinct variable names |
Typo: [] instead of () or * | Review and correct the syntax |
| Extracting digits from a number | Use str() conversion or arithmetic |
Conclusion
The TypeError: 'int' object is not subscriptable error always boils down to one thing: you're using square bracket notation on an integer, which Python doesn't allow.
The fix depends on the root cause: whether it's a simple type confusion, an inconsistent function return type, or an accidentally overwritten variable.
By keeping your variable types consistent, using descriptive variable names, and checking types when dealing with dynamic data, you can avoid this error entirely.