Introduction
The error typeerror 'builtin_function_or_method' object is not subscriptable occurs when you attempt to use the square‑bracket indexing operator ([]) on a value that Python does not consider subscriptable, such as a built‑in function. This article explains what the error means, why it happens, how to diagnose it, and how to fix it in a clear, step‑by‑step manner.
Understanding the Error
What the error means
In Python, the subscriptable protocol allows objects to be accessed with obj[index]. Only objects that implement the __getitem__ method (e.g., lists, tuples, strings, dictionaries, numpy arrays) are subscriptable. When you try to apply [] to an object that does not implement this method, Python raises a TypeError. The specific message 'builtin_function_or_method' object is not subscriptable tells you that the object you are indexing is a built‑in function or method, which lacks __getitem__.
Common scenarios
- Accidentally indexing a function call:
result = len([1, 2, 3])[0] # len returns an int, not subscriptable - Misreading a return value:
data = sorted([3, 1, 2]) # sorted returns a list, but if you assign it to a variable that later gets overwritten by a function, indexing will fail. - Using a function as a container:
def get_list(): return [10, 20, 30] first_item = get_list()[0] # This works # But if you write: first_item = get_list()[0][0] # Trying to index the result of get_list() again will raise the error if get_list() returns an int instead of a list.
Step‑by‑Step Diagnosis
Identify the problematic line
- Read the traceback – it points to the exact line where the subscript operation occurs.
- Locate the expression – look for the part that uses
[].
Check the object type
- Insert a
print(type(your_object))right before the indexing line. - If the printed type is
<class 'builtin_function_or_method'>, you have found the culprit.
Verify subscript usage
- Ask yourself: What should the expression return?
- If you expect a list or string, ensure the variable actually holds that type at runtime.
How to Fix the Issue
Convert to list or other iterable
If the function should return a collection, make sure it does:
def get_numbers():
return [1, 2, 3]
numbers = get_numbers() # numbers is now a list
first = numbers[0] # safe to index
Use appropriate attributes instead of indexing
Many built‑in functions expose data via attributes or return values rather than subscripting:
len()returns an integer; use that integer directly.range()produces a sequence, but you cannot index arangeobject directly; convert it:list(range(5))[2].
Re‑examine function calls
Sometimes the error stems from a previous line that overwrites a variable with a function result:
my_list = [1, 2, 3]
my_list = len(my_list) # my_list is now an int
my_list[0] # TypeError
Rename intermediate variables to avoid confusion:
result = len(my_list) # result is an int
my_list = [1, 2, 3] # keep the original list unchanged
Preventive Practices
-
Write defensive code – check the type before indexing:
if isinstance(obj, (list, tuple, str, dict)): value = obj[index] else: raise TypeError("Object is not subscriptable") -
Use type hints (Python 3.5+):
def process(data: list) -> None: first = data[0] # static checkers can warn if you pass a non‑list -
Run linters (e.g., pylint, flake8) – they flag questionable indexing patterns.
Scientific Explanation
Python’s type system
Python is dynamically typed, meaning the type of a variable is determined at runtime. The interpreter checks for the presence of the __getitem__ method when the [] operator is used. Built‑in functions like len, sum, or max are implemented in C and do not expose a __getitem__ method, so they are not subscriptable.
Subscriptable protocol
Any object that defines __getitem__(self, key) can be indexed. Day to day, lists, tuples, strings, dictionaries, and most custom classes follow this protocol. When a built‑in function is passed to the indexing operator, Python raises a TypeError because the protocol is not satisfied.
FAQ
Q1: Why does the error mention ‘builtin_function_or_method’ specifically?
A: The message is generated when the object’s type is identified as a built‑in function or method. Python’s traceback includes the type name to help you quickly spot the offending object That's the part that actually makes a difference..
Q2: Can I make a built‑in function subscriptable?
A: Not directly. You cannot add methods to built‑in types, but you can wrap the function’s output in a custom class that implements __getitem__ Simple, but easy to overlook..
Q3: Is this error exclusive to Python?
A: The concept of subscriptability exists in many languages, but the exact error wording is Python‑specific. Other languages may raise different exceptions (e.g., TypeError in Ruby or NotAllowedError in JavaScript) Nothing fancy..
Q4: Does this error occur with NumPy arrays?
A: No. NumPy arrays are subscriptable, so indexing a NumPy array returns an element or a slice, and the error will not appear Easy to understand, harder to ignore. Nothing fancy..
Q5: How can I avoid this mistake while debugging?
A: Print the variable’s type right before the indexing operation. If it shows builtin_function_or_method, you know the variable is a function call result that needs to be reassigned or converted.
Conclusion
The typeerror 'builtin_function_or_method' object is not subscriptable is a clear indication that you are trying to use the [] operator on a built‑in function or method, which does not support indexing. By following the diagnostic steps—identifying the problematic line, checking the object’s type, and verifying that you are indexing a truly subscriptable container—you can quickly locate and resolve the issue. Converting the function’s output to a list, using appropriate attributes, or renaming variables to prevent accidental overwrites are effective fixes. Implementing preventive practices such as type checking, defensive coding, and static analysis will help you avoid this error in future projects. Remember: the key is to see to it that the object you index implements the subscriptable protocol; otherwise, Python will raise the TypeError you have encountered.