Python provides users with a truly impressive range of options. The various data types and objects are often flexible in ways that few other languages can match. In fact, you’ll often find a wide range of built-in functionality within the most commonly used parts of the language.
However, while Python is flexible there are still firm syntactical rules to keep in mind. And a “python list object is not callable” error is a prime example of the fact that taking flexibility for granted can lead to a wide variety of problems. But as you’ll soon discover, it’s a relatively easy issue to both understand and solve.
What Does the Error Mean?
Data types in Python are usually objects. This means that data types aren’t just static entities like a + or – operator. You can expect any given data type to also have built-in methods related to its functionality. For example, Python’s strings have methods to easily change casing. But accessing information from a data type and accessing its methods are two very different things. A “python list object is not callable” error typically occurs when there’s a mismatch between a data type and how we’re trying to access it.
Specifically, the error occurs when we try to incorrectly call a list function. While this can come about in different ways, it’s most commonly encountered when we iterate through items in a list while using curly “()” rather than block “[]” brackets. Using the wrong bracket type will essentially try to call a function rather than access an element within your defined list.
A Closer Look at Python Lists and Objects
The distinction between bracket types can be better seen with a small example. Consider the following Python code.
ourList = [“alpha”, “beta”, “gamma”, “delta”]
for x in range(len(ourList)):
ourList[x] = ourList(x).title()
print(ourList)
At first glance, this code will probably seem perfectly fine. But if you run the script it’ll produce a “python list object is not callable” error. Finding the problem requires taking a closer look at how lists are defined and used.
We begin by creating a list, ourList, containing the first entries in the Greek alphabet. But what if we needed those names to be capitalized? We could take care of that fairly easily with the string title method. And the for loop should iterate through the items in ourList to modify them appropriately. But the script fails with the not callable Python error on line three. This is where a subtle problem rears its head.
We’re attempting to redefine elements of the list corresponding to x. With x equating to the number of items in ourList. But note the difference between ourList[x] and ourList(x) on line three. The first attempt to access ourList uses the correct block brackets. But the second uses curly brackets. We’re essentially telling Python’s interpreter to pass x to ourList as a parameter rather than reading its contents at position x.
But this isn’t the only way to trigger this error. You might wonder why we’re naming the list “ourList” instead of “list”. The answer can be found in the following example.
ourListItem = “a”
list = list(ourListItem)
print(list)
secondList=list(ourListItem)
print(secondList)
If you run this code you’ll receive the same error despite the fact that the code is quite dissimilar. You can see why that’s the case by going through the script’s functionality.
In the first line we define a string called ourListItem. Next, we pass ourListItem to the list constructor and assign it to a new list called list. We print out the content of list on the next line and everything seems to be working as it should. But the script exits out with the list object error when we try to repeat the process with a new list called SecondList.
In this case, we run into one of the occasional problems caused by Python’s overall flexibility. The language gives us a lot of choices with variable declaration and use. And this includes overriding most built-in names. There are only 33 reserved keywords in the language that we’re prevented from overriding. Otherwise, we can redefine almost anything in the standard lexicon – including list. So when we try to pass a variable to the list constructor it won’t work due to our earlier creation of a list named list. Now when we call the list constructor we’re actually accessing a container whose only properties are a single string.
How To Fix the Error
Thankfully fixing this error message is quite simple. With the initial example, we simply need to change the bracket style used in the loop. You can see how that would work in the following code.
ourList = [“alpha”, “beta”, “gama”, “delta”]
for x in range(len(ourList)):
ourList[x] = ourList[x].title()
print(ourList)
You should be able to run this script and see all of the list items printed to the screen with their initial letter capitalized. With the curly brackets changed to blocks the x value is now used properly to indicate position. The solution to the second example is even easier, as seen below.
ourListItem = “a”
ourList = list(ourListItem)
print(ourList)
secondList=list(ourListItem)
print(secondList)
We simply need to modify the name of the variable we assign the contents of ourListItem to. Here we go with the earlier naming convention and change list to ourList. With that completed the script now runs properly and prints out the contents of ourList and secondList. This is due to the fact that the variable named list is no longer present and overwriting the standard list functionality.