![]() |
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.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.
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 ParenthesesIn below code Misuse of parentheses instead of square brackets for list indexing, causing a “‘list‘ object is not callable” error.
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.
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 PythonBelow, are the approaches to solve Python “‘list’ object is not callable” in Python
Correct Variable NameIn 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.
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 ParenthesesTo access a list item, always use square brackets, as used in the below code.
Output Ruler Rename Conflict method and AttributeBy 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.
Output [95, 67, 81, 64, 87] |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |