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
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. Python is: A high-level language, used in web development, data science, automat
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow. Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples. The below Python section contains a wide collection of Python programming examples. These Python c
11 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
10 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list(). Let's look at a simple exa
3 min read