How to fix "'list' object is not callable" in Python
Last Updated :
28 Mar, 2024
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])
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]
Similar Reads
How to fix - "typeerror 'module' object is not callable" in Python
Python is well known for the different modules it provides to make our tasks easier. Not just that, we can even make our own modules as well., and in case you don't know, any Python file with a .py extension can act like a module in Python. In this article, we will discuss the error called "typeerr
4 min read
How to Fix "TypeError: 'float' object is not callable" in Python
In Python, encountering the error message "TypeError: 'float' object is not callable" is a common issue that can arise due to the misuse of the variable names or syntax errors. This article will explain why this error occurs and provide detailed steps to fix it along with the example code and common
4 min read
How to Fix "TypeError: 'Column' Object is Not Callable" in Python Pandas
The error "TypeError: 'Column' object is not callable" in Python typically occurs when you try to call a Column object as if it were a function. This error commonly arises when working with the libraries like Pandas or SQLAlchemy. However, users often encounter various errors when manipulating data,
3 min read
How to create a list of object in Python class
In Python, creating a list of objects involves storing instances of a class in a list, which allows for easy access, modification and iteration. For example, with a Geeks class having attributes name and roll, you can efficiently manage multiple objects in a list. Let's explore different methods to
3 min read
How to Fix: TypeError: ânumpy.floatâ object is not callable?
In this article, we are going to see how to fix TypeError: ânumpy.floatâ object is not callable in Python. There is only one case in which we can see this error: If we try to call a NumPy array as a function, we are most likely to get such an error. Example: C/C++ Code import numpy as np a = np.arra
1 min read
How to Fix TypeError: 'NoneType' object is not iterable in Python
Python is a popular and versatile programming language, but like any other language, it can throw errors that can be frustrating to debug. One of the common errors that developers encounter is the "TypeError: 'NoneType' object is not iterable." In this article, we will explore various scenarios wher
8 min read
Fix 'Int' Object is Not Subscriptable in Python
In this article, we will study how to fix 'int' object that is not subscriptable in Python. But before that let us understand why it occurs and what it means. What is 'Int' Object Is Not Subscriptable Error?The error 'int' object is not subscriptable occurs when you attempt to use indexing or slicin
5 min read
How to Find Length of a list in Python
The length of a list means the number of elements it contains. In-Built len() function can be used to find the length of an object by passing the object within the parentheses. Here is the Python example to find the length of a list using len(). [GFGTABS] Python a1 = [10, 50, 30, 40] n = len(a1) pri
3 min read
How to check if an object is iterable in Python?
In simple words, any object that could be looped over is iterable. For instance, a list object is iterable and so is an str object. The list numbers and string names are iterables because we are able to loop over them (using a for-loop in this case). In this article, we are going to see how to check
3 min read
How to Split a File into a List in Python
In this article, we are going to see how to Split a File into a List in Python. When we want each line of the file to be listed at consecutive positions where each line becomes an element in the file, the splitlines() or rstrip() method is used to split a file into a list. Let's see a few examples t
5 min read