Last updated: Apr 10, 2024
Reading timeยท4 min
The Python "TypeError: 'function' object is not subscriptable" occurs when we call a function using square brackets or declare a function with the same name as a variable.
To solve the error, call functions using parentheses and make sure the names of functions and variables don't clash.
A common cause of the error is calling a function with square brackets instead of parentheses.
def greet(name): return 'hello ' + name # โ๏ธ TypeError: 'function' object is not subscriptable result = greet['bobby'] # ๐๏ธ Should be parentheses not square brackets
Notice that we used square brackets to call the greet()
function.
def greet(name): return 'hello ' + name result = greet('bobby') print(result) # ๐๏ธ 'hello bobby'
Parentheses are used to call a function or method, whereas square brackets are used to access a key in a dictionary or an item in a list.
One or more comma-separated arguments can be passed in the call to the function between the parentheses.
def greet(first, last): return 'hello ' + first + ' ' + last result = greet('bobby', 'hadz') print(result) # ๐๏ธ 'hello bobby hadz'
If you are passing a list as an argument to a function, use parentheses to call the function and pass the list between the parentheses.
def get_list(a_list): return a_list + ['last'] result = get_list(['bobby', 'hadz']) print(result) # ๐๏ธ ['bobby', 'hadz', 'last']
You can use square brackets if you need to access:
The syntax for list slicing is
my_list[start:stop:step]
.
a_list = ['bobby', 'hadz', 'com'] print(a_list[0]) # ๐๏ธ bobby print(a_list[-1]) # ๐๏ธ com print(a_list[:2]) # ๐๏ธ ['bobby', 'hadz']
The start
index is inclusive and the stop
index is exclusive (up to, but not
including).
start
index is omitted, it is considered to be 0
, if the stop
index is omitted, the slice goes to the end of the list.Python indexes are zero-based, so the first item in a list has an index of 0
,
and the last item has an index of -1
or len(my_list) - 1
.
The syntax for string slicing is the same.
You can also use square brackets to access a key in a dictionary.
my_dict = { 'site': 'bobbyhadz.com', 'topic': 'Python' } print(my_dict['site']) # ๐๏ธ bobbyhadz.com print(my_dict['topic']) # ๐๏ธ Python
If you need to get a value that is returned from a function and access it at a specific index:
def get_list(): return ['bobby', 'hadz', '.', 'com'] a_list = get_list() print(a_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com'] first = get_list()[0] print(first) # ๐๏ธ bobby
Here is another example of how the error occurs.
numbers = [2, 4, 6] def numbers(): return [2, 4, 6] # โ๏ธ TypeError: 'function' object is not subscriptable print(numbers[0]) # ๐๏ธ function and variable name clash
The error is most commonly caused when we have a function and a variable with the same name.
The numbers
function overrides the numbers
variable, so when we use square
brackets to access the variable, we are actually trying to access the function
at index 0
.
numbers = [2, 4, 6] def numbers(): return [2, 4, 6] # โ๏ธ TypeError: 'function' object is not subscriptable print(numbers[0]) # ๐๏ธ function and variable name clash
To solve the error, rename the function or the variable, so their names don't clash.
numbers = [2, 4, 6] # โ function now has a different name def get_numbers(): return [2, 4, 6] print(numbers[0]) # ๐๏ธ 2 result = get_numbers() print(result[0]) # ๐๏ธ 2
We renamed the function to get_numbers
so it no longer clashes with the
variable and the error is solved.
The "object is not subscriptable" TypeError basically means that the object cannot be accessed using square brackets.
You should only use square brackets to access subscriptable objects.
The subscriptable
objects in Python are:
All other objects have to be converted to a subscriptable object by using the
list(),
tuple(), dict()
or
str() classes to be able to use bracket
notation.
Subscriptable objects implement the __getitem__
method whereas
non-subscriptable objects do not.
a_list = ['c', 'b', 'a'] # ๐๏ธ <built-in method __getitem__ of list object at 0x7f71f3252640> print(a_list.__getitem__)
I've also written an article on how to check if a variable is a function.
You can learn more about the related topics by checking out the following tutorials: