TypeError: ‘list’ object is not callable

In this post, we will see how to resolve TypeError: ‘list’ object is not callable in python.
Let’s first understand why we get this error.

In python, a built-in name is a name in which the python interpreter assigns predefined value. The value can be objects, functions or classes. All these built-in names will be available irrespective of the scope.

Please note that Python won’t stop you from reassigning these built-in names. Built-in names are not reserved and you can use them in your program as variable names as well.

For example:
set = {}
print(“Empty dict with variable name:”,set)

Output:
Empty dict with variable name: {}

As you can see, we are able to create a variable name set and assign an empty dictionary to it.

So this is a very common error and you should avoid overriding any built-in names such as list, set or dict etc.PEP8 provides recommendations on naming a variable.

Let’s see this with help of example:

Output:

Traceback (most recent call last):
File “main.py”, line 7, in
print(list(set1))
TypeError: ‘list’ object is not callable

As you can see, we have created a variable name list and assigned list to it. This list variable has overriding predefined list built-in name, so when we are using list function to convert set to list, it is raising an error.

Solution 1

To solve about the issue, let’s rename list variable to list1

Output:

[1, 2, 2, 3, 4, 2, 1, 1, 4] [1, 2, 3, 4, 5]

As you can see, this resolved the issue.

Solution 2

In case you are using any interactive python session, use this.

If the above solution still did not work for you, there can be one more reason for this issue.

Solution 3

If you are trying to access list index by () rather than [], then you might get this issue as well.

Output:

Traceback (most recent call last):
File “main.py”, line 2, in
print(list1(2))
TypeError: ‘list’ object is not callable

Change list(2) to list[2] and it will work fine.

Output:

Bhutan

That’s all about TypeError: ‘list’ object is not callable in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *