Skip to main content

How to Resolve Python "TypeError: 'function' object is not subscriptable"

The TypeError: 'function' object is not subscriptable is a common Python error indicating that you've tried to use square bracket notation ([]) on a function object itself, as if you were trying to access an element by index or key. Functions are callable code blocks, not data containers like lists or dictionaries that support subscripting.

This guide explains the main causes for this error – typically confusing function calls with indexing or name conflicts – and provides clear solutions.

Understanding the Error: Functions vs. Subscriptable Objects

  • Functions: Defined using def or lambda. They represent blocks of executable code. You execute them by calling them with parentheses: my_function(arguments). Function objects themselves are not designed to be accessed by index or key.
  • Subscriptable Objects: Data types that allow accessing their internal elements using square brackets []. These include:
    • list: Access by numerical index (e.g., my_list[0]).
    • tuple: Access by numerical index (e.g., my_tuple[1]).
    • str: Access individual characters by numerical index (e.g., my_string[-1]).
    • dict: Access values by key (e.g., my_dict['key']).

The TypeError: 'function' object is not subscriptable occurs because you applied the subscripting operation ([]) directly to an object that is a function, not one of the subscriptable data types.

Cause 1: Using Square Brackets [] Instead of Parentheses () to Call a Function

This is a frequent syntax mistake. You intend to call a function but accidentally use square brackets instead of the required parentheses.

def calculate_total(a, b):
"""Adds two numbers."""
return a + b

# Error Scenario: Using [] to call the function
try:
# ⛔️ TypeError: 'function' object is not subscriptable
# Trying to pass arguments using square brackets
result = calculate_total[5, 10]
print(result)
except TypeError as e:
print(e)

Python interprets calculate_total[5, 10] as trying to access an index or key (5, 10) within the function object calculate_total, which is not allowed.

Solution 1: Use Parentheses () to Call Functions

Always use parentheses () to call functions and pass arguments inside them.

def calculate_total(a, b):
"""Adds two numbers."""
return a + b

# Solution: Use parentheses () to call the function
result = calculate_total(5, 10) # Pass arguments inside parentheses

print(f"Function call result: {result}") # Output: Function call result: 15

# Example with one argument
def process_name(name):
return f"Processing {name}"

processed_name = process_name("Alice") # Call with ()
print(processed_name) # Output: Processing Alice

Output:

Function call result: 15
Processing Alice

Cause 2: Name Conflict Between a Function and a Variable

If you define a function and then later (or in the same scope) create a variable with the exact same name, the variable name overrides (shadows) the function name. If you then try to use square brackets ([]) intending to access the original variable (which might have been subscriptable, like a list), you will actually be attempting to subscript the function object that the name now refers to.

# Variable definition (intended to be accessed)
user_list = ["admin", "editor", "viewer"]

# Function definition with the SAME NAME as the variable
def user_list(): # This function now hides the list above
print("This is the user_list function!")
return ["guest"]

# Error Scenario: Trying to access the list, but name refers to the function
try:
# ⛔️ TypeError: 'function' object is not subscriptable
# 'user_list' now refers to the function, not the list.
# Trying user_list[0] attempts to index the function object.
first_user = user_list[0]
print(first_user)
except TypeError as e:
print(e)

# You *can* still call the function using parentheses
function_result = user_list() # Calls the function
print(f"Function result: {function_result}") # Output: ['guest']

Output:

'function' object is not subscriptable
This is the user_list function!
Function result: ['guest']

Solution 2: Rename the Function or the Variable

To avoid this conflict, use distinct names for your variables and functions.

# ✅ Use distinct names
users = ["admin", "editor", "viewer"] # Variable name changed

def get_user_list(): # Function name changed
print("This is the get_user_list function!")
return ["guest"]

# Accessing the list variable now works correctly
first_user = users[0]
print(f"First user from list: '{first_user}'") # Output: First user from list: 'admin'

# Calling the renamed function also works correctly
function_result = get_user_list()
print(f"Result from function call: {function_result}") # Output: Result from function call: ['guest']

Output:

First user from list: 'admin'
This is the get_user_list function!
Result from function call: ['guest']

Choose clear, distinct names to prevent shadowing.

Accessing Elements Returned by a Function

If a function returns a subscriptable object (like a list or string), you first call the function using parentheses () to get the returned object, and then use square brackets [] on the result.

def get_data():
"""Returns a list of sensor readings."""
return [10.5, 11.2, 10.9, 11.5]

# Call the function first to get the list
data_list = get_data()
print(f"List returned by function: {data_list}") # Output: [10.5, 11.2, 10.9, 11.5]

# Now access elements of the returned list using []
first_reading = data_list[0]
print(f"First reading: {first_reading}") # Output: 10.5

# You can chain the call and the index access:
second_reading = get_data()[1] # Call get_data(), then access index 1 of the result
print(f"Second reading (chained): {second_reading}") # Output: 11.2

Output:

List returned by function: [10.5, 11.2, 10.9, 11.5]
First reading: 10.5
Second reading (chained): 11.2

When to Use Square Brackets ([])

Remember, square brackets [] are used for:

  • Accessing list/tuple/string elements by numerical index: my_list[0], my_string[-1]
  • Slicing sequences: my_list[1:3], my_string[:-2]
  • Accessing dictionary values by key: my_dict['user_name']

They are not used for calling functions.

Conclusion

The TypeError: 'function' object is not subscriptable error arises from incorrectly trying to use square bracket indexing ([]) on a function object.

The primary solutions are:

  1. Use parentheses () to call functions, passing arguments inside them: my_function(arg1, arg2).
  2. Ensure distinct names for functions and variables to avoid accidentally shadowing a function with a variable (or vice-versa) when you intend to use subscripting on the variable.

If a function returns a list, dictionary, or other subscriptable object, call the function first (result = my_func()) and then apply the square brackets to the result.