How to solve TypeError “‘list’ object is not callable” in Python

Sometimes you will get the error “‘list’ object is not callable” in Python because you make mistakes such as calling a list as a function or naming a class property with the same name as the class method. This tutorial will give you solutions to each mistake. Read carefully.

Reason for “TypeError: ‘list’ object is not callable” in Python

The error “‘list’ object is not callable” occurs when you try to call a list object as if it were a function. This can happen if you have assigned a list to a variable with the same name as a built-in function. Here is an example of how this error can occur:

Code:

class Student():
    def __init__(self, grade, subjects):
        self.grade = grade
        self.subject = subjects

    def getGrade(self):  # <-- Error here
        return self.grade

    def subject(self):
        return self.subjects

aStudent = Student(5, ['Math', 'Literature', 'Music'])
print(aStudent.subject())

Result:

Traceback (most recent call last):
  line 13, in <module>
    print(aStudent.subject())
TypeError: 'list' object is not callable

Also, the error occurs when you use the parenthesis when accessing the index of an element:

Code:

myList = [22, 6, 3, 11, 23, 7, 4]
 
# Error when using the parenthesis to access the index
print("The element in the index 2 is:", myList(2))

Result:

Traceback (most recent call last):
  line 4, in <module>
    print("The element in the index 2 is:", myList(2))
TypeError: 'list' object is not callable

Solution to the error

Rename the class method 

When the class method and the class property have the same name, the calling will be prior to accessing the class property. And because the class property is a list of values, the error appears. So, you must change the name of the class method to ensure that the error disappears. Our previous article discussed the similar error about the NoneType object, take a visit to understand more.

Code:

class Student():
    def __init__(self, grade, subjects):
        self.grade = grade
        self.subject = subjects

    def getGrade(self):
        return self.grade

# Rename the class method
    def getSubject(self):
        return self.subject

aStudent = Student(5, ['Math', 'Literature', 'Music'])
print(aStudent.getSubject())

Result:

['Math', 'Literature', 'Music']

Use the square brackets to access the index

The correct syntax to access an index is using the square brackets with a number inside. You may need to correct a mistake when using the parenthesis. Change the brackets, and the error will disappear.

Code:

myList = [22, 6, 3, 11, 23, 7, 4]
 
# Use the square brackets [] to access the index
print("The element in the index 2 is:", myList[2])

Result:

The element in the index 2 is: 3

Summary

In summary, the error “TypeError: ‘list’ object is not callable” in Python occurs when you call a list object as a function in object-oriented programming or access the index by the parenthesis. To avoid the error, remember to name the class method differently from the class property and use the square brackets whenever accessing the index.

Leave a Reply

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