Last modified: Feb 11, 2026 By Alexander Williams
Fix Python Object is Not Subscriptable Error
You are writing Python code. You try to access an item using square brackets []. Suddenly, you see a red error message: "TypeError: 'YourObject' object is not subscriptable."
This error is common for beginners. It can be confusing at first. This guide will explain what it means. We will show you why it happens. You will learn how to fix it for good.
What Does "Not Subscriptable" Mean?
In simple terms, "subscriptable" means you can use square brackets on an object. You can get an item by its index or key. Lists, tuples, dictionaries, and strings are subscriptable.
A "not subscriptable" error means you tried this on an object that does not support it. Python is telling you that this specific object type cannot be accessed with [].
The error always looks like this:
TypeError: 'int' object is not subscriptable
The word before 'object' (like 'int') tells you the type causing the problem.
Why This Error Happens: Common Causes
The root cause is a mismatch. You are treating an object like a sequence (list, tuple) or a mapping (dictionary). But the object is something else.
1. Using [] on Basic Data Types
Integers, floats, Booleans, and None are not subscriptable. You cannot index into the number 42.
# Example causing the error
my_number = 42
first_digit = my_number[0] # TypeError!
TypeError: 'int' object is not subscriptable
2. Confusing an Object with a List or Dict
This often happens with functions that return different types. Or when you forget to call a method that returns a list.
# Example: .items() returns a special view object
my_dict = {"a": 1, "b": 2}
items_object = my_dict.items() # This is a dict_items object
first_item = items_object[0] # TypeError! Not a list.
To fix it, convert the object to a list first: list(my_dict.items())[0].
3. Custom Class Objects Without __getitem__
By default, objects from your own classes are not subscriptable. You must define the __getitem__ method. This method tells Python how to handle [].
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
my_book = Book("Python 101", "Jane Doe")
# This will fail because Book lacks __getitem__
chapter = my_book[1]
To make a class subscriptable, you need to understand Python Objects: Classes, Instances, and Methods. This guide covers the fundamentals of creating and using custom objects.
How to Fix the Error: Step-by-Step Guide
Follow these steps when you see the error.
Step 1: Identify the Object Type
Look at the error message. It says 'int' object or 'function' object. Use the type() function to check your variable.
my_var = 10
print(type(my_var)) # Output: Step 2: Check Your Assumptions
Did you think the variable was a list? Print it. See what it really is.
result = some_function()
print(f"Result is: {result}")
print(f"Type is: {type(result)}")
Step 3: Apply the Correct Access Method
Use the right tool for the object.
- Lists/Tuples/Strings: Use
[]with an index (e.g.,my_list[0]). - Dictionaries: Use
[]with a key (e.g.,my_dict["key"]). - Objects: Use dot notation for object attributes (e.g.,
obj.attribute). - Special Objects (like dict_items): Convert to a list first:
list(obj).
Practical Examples and Solutions
Example 1: Fixing a Function Return Value
def get_data():
# This returns a tuple, but let's pretend we forgot
return "filename.txt", 1024
data = get_data()
# ERROR: We think 'data' is a list or tuple with a 'size' key
# size = data["size"]
# SOLUTION: It's a tuple. Access by index.
filename = data[0]
filesize = data[1]
print(f"File: {filename}, Size: {filesize} bytes")
File: filename.txt, Size: 1024 bytes
Example 2: Making a Custom Class Subscriptable
Define the __getitem__ method. This makes your object behave like a sequence.
class Playlist:
def __init__(self, songs):
self.songs = list(songs) # Store songs internally as a list
def __getitem__(self, index):
# This method enables the [] operator
return self.songs[index]
def __repr__(self):
return f"Playlist({self.songs})"
my_playlist = Playlist(["Song A", "Song B", "Song C"])
print(my_playlist[1]) # Now this works!
print(my_playlist[:2]) # Slicing works too!
Song B
['Song A', 'Song B']
This concept is a building block for more complex object inheritance patterns, where a child class can inherit subscriptable behavior.
Example 3: Handling JSON Data Correctly
When you load JSON data, you get Python lists and dictionaries. They are subscriptable. But if you parse it wrong, you might get a string.
import json
json_string = '{"name": "Alice", "hobbies": ["reading", "coding"]}'
# Correct: json.loads converts string to a dict
parsed_data = json.loads(json_string)
print(parsed_data["name"]) # Works
print(parsed_data["hobbies"][1]) # Works
# Incorrect: Treating the original string as a dict
# print(json_string["name"]) # This would cause a TypeError!
For more on this process, see our guide on converting JSON to Python objects.
Conclusion
The "object is not subscriptable" error is a clear signal. You are trying to use square brackets on an object that does not support it.
First, check the object type with type(). Then, use the correct access method. For custom classes, implement __getitem__ to enable subscription.
Understanding this error deepens your knowledge of Python's type system. It helps you write more robust and predictable code. Remember, not everything in Python is a list or dictionary. Choose the right tool for the data you have.