Open In App

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

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A list is also an object that is used to store elements of different data types. It is common to see the error “‘list’ object is not callable” while using the list in our Python programs. In this article, we will learn why this error occurs and how to resolve it.

What does it mean by ‘list’ object is not callable in Python?

The “‘list’ object is not callable” error is a common runtime error encountered by Python developers. It occurs when you try to call a list object as if it were a function. In Python, lists are objects that hold an ordered collection of items. They are accessed using indexing or slicing, not by calling them like functions.

Syntax

 "'list' object is not callable" 

Below, are the reasons for occurring for Python “‘ list’ object is not callable” in Python:

  • Variable Name Conflicts with Function.
  • Misuse of Parentheses
  • Method clashes with Property.

Variable Name Conflicts with Function.

In the below code, list is assigned as a variable name, conflicting with the built-in function list(). To resolve this, either rename the variable or explicitly access the list() function from the builtins module.

Python3
# Create a list
list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Create a list of quantity
moreNumbers = list(range(10,21))

# Print the list and quantity
print(list)
print(moreNumbers)

Output

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 4, in <module>
    moreNumbers = list(range(10,21))
TypeError: 'list' object is not callable

Misuse of Parentheses

In below code Misuse of parentheses instead of square brackets for list indexing, causing a “‘list‘ object is not callable” error.

Python3
items = ["Pencil", "Eraser", "Gel Pen", "Ruler"]

print(items(3))

Output

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 3, in <module>
    print(items(3))
TypeError: 'list' object is not callable

Method clashes with Property.

In the below example, the error occurs due to a naming conflict between the method “marks()” and the property marks. Here, the Student class has an attribute marks, which is initially assigned as a list containing the student’s marks. However, the class also defines a method, “marks()”, which returns the same list of marks.

Python3
class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def marks(self):
        return self.marks

student = Student("Lalit", [95, 67, 81, 64, 87])
# Generates the error
print(student.marks())  

Output

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 11, in <module>
    print(student.marks())  
TypeError: 'list' object is not callable

Solution for Python “‘list’ object is not callable” in Python

Below, are the approaches to solve Python “‘list’ object is not callable” in Python

  • Correct Variable Name
  • Correct use of Parentheses
  • Rename Conflict method and Attribute

Correct Variable Name

In the below solution rename the variable “list” to avoid shadowing built-in function names. Use square brackets for indexing instead of parentheses to access elements in the list. Print both lists to display their contents.

Python3
# Create a list
one2nine = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Create a list of quantity
moreNumbers = list(range(10,21))

# Print the list and quantity
print(one2nine)
print(moreNumbers)

Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Correct use of Parentheses

To access a list item, always use square brackets, as used in the below code.

Python3
items = ["Pencil", "Eraser", "Gel Pen", "Ruler"]

print(items[3])

Output
Ruler

Rename Conflict method and Attribute

By renaming the property to “marks” and the method to “get_marks()”, we will eliminate the naming conflict, ensuring that “student.get_marks()” retrieves the list of marks without encountering the TypeError.

Python3
class Student:
    def __init__(self, name, marks):
        self.name = name
        # Change the property name to _marks
        self._marks = marks  
    # Rename the method to get_marks
    def get_marks(self):  
        return self._marks

student = Student("Lalit", [95, 67, 81, 64, 87])
# Outputs the list of marks
print(student.get_marks())  

Output
[95, 67, 81, 64, 87]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads