TypeError: 'function' object is not subscriptable in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# TypeError: 'function' object is not subscriptable in Python

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.

typeerror function object is not subscriptable

# Use parentheses to call functions in Python

A common cause of the error is calling a function with square brackets instead of parentheses.

main.py
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.

To solve the error, only call functions using parentheses.
main.py
def greet(name): return 'hello ' + name result = greet('bobby') print(result) # ๐Ÿ‘‰๏ธ 'hello bobby'

call function using parentheses

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.

# Passing a list as an argument to a function

One or more comma-separated arguments can be passed in the call to the function between the parentheses.

main.py
def greet(first, last): return 'hello ' + first + ' ' + last result = greet('bobby', 'hadz') print(result) # ๐Ÿ‘‰๏ธ 'hello bobby hadz'

passing list as argument to function

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.

main.py
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:

  • a list at a specific index
  • a tuple at a specific index
  • a string at a specific index
  • a specific key in a dictionary

The syntax for list slicing is my_list[start:stop:step].

main.py
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).

If the 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.

# Use square brackets to access a key in a dictionary

You can also use square brackets to access a key in a dictionary.

main.py
my_dict = { 'site': 'bobbyhadz.com', 'topic': 'Python' } print(my_dict['site']) # ๐Ÿ‘‰๏ธ bobbyhadz.com print(my_dict['topic']) # ๐Ÿ‘‰๏ธ Python

use square brackets to access key in dictionary

# Accessing a value that is returned from a function

If you need to get a value that is returned from a function and access it at a specific index:

  1. Use parentheses to call the method.
  2. Use square brackets to access the value at an index.
main.py
def get_list(): return ['bobby', 'hadz', '.', 'com'] a_list = get_list() print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com'] first = get_list()[0] print(first) # ๐Ÿ‘‰๏ธ bobby

access value that is returned from function

# Having a function and a variable with the same name

Here is another example of how the error occurs.

main.py
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.

main.py
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.

main.py
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:

  • list
  • tuple
  • dictionary
  • string

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.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev