Using parenthesis instead of square brackets to index the elements of a list is the most common cause of TypeError: ‘list’ object is not callable in Python.
The purpose of this tutorial is to explain what the TypeError ‘list’ object is not callable error means and how to resolve this TypeError in your program using practical examples.
Fix Python TypeError: ‘list’ object is not callable
In Python, there are two main scenarios in which you will encounter the ‘list’ object that is not a callable error. Let’s take a look at both scenarios and provide examples for each.
Reason 1: Using a variable name from the built-in name list
The most common mistake that developers make is to declare the names of Python built-in functions or methods as variable names in their code.
For the purposes of this article, a built-in name is simply a name to which the Python interpreter has already given an assigned predefined value. There are two types of values: functions and class objects.
The Python interpreter comes pre-loaded with more than 70 functions and types that are always accessible.
When it comes to Python, the list is a built-in function, and it is not recommended that you use the built-in functions or keywords to name your variables.
Using built-in names as variable names is not prohibited by Python, but doing so results in the function losing its ability to function and instead of acting as a standard variable, which is not desirable.
Let us see in the below code example what can cause the error and how you can fix it.
#Initializing a integer list = [0] #Printing the Int value print(list) #Creating String charVal = "GetString" #Convert String to List newList = list(charVal) print(newList)
Output:
[0]
Traceback (most recent call last):
File "c:\Users\ravi9\Coduber\test.py", line 11, in <module>
newList = list(charVal)
TypeError: 'list' object is not callable
As an example, consider the code in the preceding paragraph. We’ve declared a list variable with the value stored as a list with a value 0.
Because we have used the word “list” as the name of a variable, in this case, the list() method will lose its properties and functionality and will behave as if it were just another variable.
A new string variable named charVal is declared, and when we attempt to convert it into a list by creating another list, we receive the TypeError: The ‘list’ object is not the callable error message.
This TypeError occurs because we have a list variable that is no longer a built function because we re-assigned the built-in name list in the script, and this is the cause of the error. Thus, the predefined list value, which is an instance of a class object representing a Python list, is no longer available for use.
To resolve this issue, rename the list variable at line 2 to a different name and see if the code example below runs properly or not with any errors.
#Initializing a integer listNum = [0] #Printing the Int value print(listNum) #Creating String charVal = "GetString" #Convert String to List newList = list(charVal) #Print the new List print(newList)
Output:
[0]
['G', 'e', 't', 'S', 't', 'r', 'i', 'n', 'g']
As you can see in the code above, I was able to successfully convert the string variable into a list, and it printed the list of characters contained within the list.
Reason 2: Accessing List Item Using Small Parenthesis
In addition, if you are trying to index an array of elements by using parenthesis() instead of square brackets [], you will most likely receive this error. The elements of a list are accessed by using the square brackets with the index number to get to that specific element in the list.
Look at the code example below to see why this issue occurs and how you can resolve it if this occurs.
#Initializing a List newList = [1,2,3,4] #Access First Element and Try to Print print(newList(0))
Output:
Traceback (most recent call last):
File "c:\Users\ravi9\Coduber\test.py", line 5, in <module>
print(newList(0))
TypeError: 'list' object is not callable
In the preceding program, we have a list of numbers called “newList,” and we are accessing and printing the first element of the list by indexing the list using the parenthesis newList(0), but this is incorrect. The Python interpreter will throw a TypeError: ‘list’ object is not callable error if this condition is met.
Let’s look at how you can fix this error. As you’ve probably noticed, you can’t use small brackets to access the elements of the list, and you’ll need to use square brackets to access any element in the list.
#Initializing a List newList = [1,2,3,4] #Access First Element and Try to Print print(newList[0])
Output:
1
Wrap Up
In two different scenarios, the TypeError: ‘list’ object is not callable error is thrown. The use of parenthesis rather than square brackets to access elements of a list will result in an error. If you try to use built-in names as variable names, such as list, you will get an error.
This is a common mistake made by most developers when indexing the elements of a list or when using the built-in names of variables as variable names.
Let me know in the comment section if you are still facing this error or you have a better solution than the one discussed here I will be happy to add it here or help you.
Further Read: