When you try to access items in a list using curly brackets ( () ), Python returns an error. This is because Python thinks that you are trying to call a function.
In this guide, we talk about the Python “typeerror: ‘list’ object is not callable” error and why it is raised. We’ll walk through an example scenario to help you learn how to fix this error. Let’s begin!
The Problem: typeerror: ‘list’ object is not callable
Python already tells us all we need to know in this error:
typeerror: 'list' object is not callable
Take a look at the error type: TypeError. This is one of the most common types of Python errors. It tells us we’re trying to manipulate a value using a method not available to the type of data in which the value is stored.
Our error message tells us that we’re trying to call a Python list object. This means we are treating it like a function rather than as a list.
This error is raised when you use curly brackets to access items in a list. Consider the following list of scones:
scones = ["Cherry", "Apple and Cinnamon", "Plain", "Cheese"]
To access items in this list, we must state the index number of the value we want to access enclosed by square brackets:
This returns: Cherry. 0 is the position of the first item in our list, “Cherry”.
An Example Scenario
We’re going to build a Python program that capitalizes a list of names. We must capitalize the first letters of these names because they are going to be printed out on name cards.
Start by declaring a list of names:
names = ["Peter Geoffrey", "Dakota Williams", "Rebecca Lee"]
Next, create a for loop that iterates through this list of names. We’ll convert each name to upper case and replace the lowercase name with the uppercase name in the list:
for n in range(len(names)): names[n] = names(n).upper() print(names(n)) print(names)
Use the range() method to iterate through every item in the “names” list. Then use the assignment operator to change the value of each name to its uppercase equivalent. The Python upper() method converts each name to uppercase.
Next, print out the new name to the console. Once our loop has run, print out the whole revised list to the console.
Let’s run our code:
Traceback (most recent call last): File "main.py", line 4, in <module> names[n] = names(n).upper() TypeError: 'list' object is not callable
We’ve received an error, as expected. Let’s solve this problem.
Use square brackets to access items in a list. Curly brackets are used to call functions in Python. The problem in our code is that we’re trying to call a list as a function because we’re using curly brackets to access items in our list.
In our code, use curly brackets to access a list item in two places:
for n in range(len(names)): names[n] = names(n).upper() print(names(n))
We must swap the names(n) code to use square brackets:
for n in range(len(names)): names[n] = names[n].upper() print(names[n])
This tells Python we want to access the item at the index position “n” in the list “names”.
Run our code with the applicable revisions we just discussed:
PETER GEOFFREY DAKOTA WILLIAMS REBECCA LEE ['PETER GEOFFREY', 'DAKOTA WILLIAMS', 'REBECCA LEE']
This time, a successful response returns. Every name is converted into capital letters.
The version of a name in capital letters replaces the sentence-case version of the name. Then, we print out each name to the console. When our program is done, we print out a list of all the names in “names” to check that they have been changed in our list.
The Python “typeerror: ‘list’ object is not callable” error is raised when you try to access a list as if it were a function. To solve this error, make sure square brackets are used to access or change values in a list rather than curly brackets.
Now you’re ready to fix this error in your code like a professional Python developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more