Solve - TypeError: 'list' object is not callable in Python

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

Solve - TypeError: 'list' object is not callable in Python #

The Python "TypeError: 'list' object is not callable" occurs when we try to call a list as a function using parenthesis (). To solve the error, make sure to use square brackets when accessing a list at a specific index, e.g. my_list[0].

typeerror list object is not callable

Here is one example of how the error occurs.

main.py
my_list = ['a', 'b', 'c'] # ⛔️ TypeError: 'list' object is not callable print(my_list(0)) # 👈️ uses parenthesis instead of square brackets

The issue in the code sample is that we used parenthesis instead of square brackets when accessing the list at a specific index.

main.py
my_list = ['a', 'b', 'c'] print(my_list[0]) # 👉️ "a" print(my_list[1]) # 👉️ "b"

Another thing to look out for is that you're not overriding the built-in list() function.

main.py
# 👇️ overriding built-in function list = ['a', 'b', 'c'] # ⛔️ TypeError: 'list' object is not callable print(list([1, 2, 3]))

To solve the error in this situation, rename your variable and restart your script.

main.py
# ✅ not overriding built-in list anymore my_list = ['a', 'b', 'c'] print(list([1, 2, 3]))

Another common cause of the error is simply trying to call a list as a function.

main.py
my_list = ['a', 'b', 'c'] # ⛔️ TypeError: 'list' object is not callable print(my_list())

To solve the error, you either have to remove the parenthesis or figure out how the variable got assigned a list instead of a function or a class.

The error is also caused when we have a class method and a class property with the same name.

main.py
class Employee(): def __init__(self, tasks): # 👇️ this attribute hides the method self.tasks = tasks # 👇️ same name as class variable def tasks(self): return self.tasks emp = Employee(['dev', 'test']) # ⛔️ TypeError: 'list' object is not callable print(emp.tasks())

The Employee class has a method and an attribute with the same name.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the object is not callable error.

To solve the error, you have to rename the class method.

main.py
class Employee(): def __init__(self, tasks): self.tasks = tasks def get_tasks(self): return self.tasks emp = Employee(['dev', 'test']) print(emp.get_tasks()) # 👉️ ['dev', 'test']

Once you rename the method, you will be able to call it without any issues.

Make sure you don't have a function and a variable with the same name.

main.py
def example(): return 'hello world' # 👇️ shadowing function example = ['a', 'b', 'c'] # ⛔️ TypeError: 'list' object is not callable print(example())

The example variable shadows the function with the same name, so when we try to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

Use the search field on my Home Page to filter through my more than 1,000 articles.