ItsMyCode |
The most common scenario where Python throws TypeError: ‘list’ object is not callable is when you have assigned a variable name as “list” or if you are trying to index the elements of the list using parenthesis instead of square brackets.
In this tutorial, we will learn what ‘list’ object is is not callable error means and how to resolve this TypeError in your program with examples.
Python TypeError: ‘list’ object is not callable
There are two main scenarios where you get a ‘list’ object is not callable error in Python. Let us take a look at both scenarios with examples.
Scenario 1 – Using the built-in name list as a variable name
The most common mistake the developers tend to perform is declaring the Python built-in names or methods as variable names.
What is a built-in name?
In Python, a built-in name is nothing but the name that the Python interpreter already has assigned a predefined value. The value can be either a function or class object.
The Python interpreter has 70+ functions and types built into it that are always available.
In Python, a list
is a built-in function, and it is not recommended to use the built-in functions or keywords as variable names.
Python will not stop you from using the built-in names as variable names, but if you do so, it will lose its property of being a function and act as a standard variable.
Let us take a look at a simple example to demonstrate the same.
fruit = "Apple"
list = list(fruit)
print(list)
car="Ford"
car_list=list(car)
print(car_list)
Output
['A', 'p', 'p', 'l', 'e']
Traceback (most recent call last):
File "c:\Personal\IJS\Code\main.py", line 6, in <module>
car_list=list(car)
TypeError: 'list' object is not callable
If you look at the above example, we have declared a fruit variable, and we are converting that into a list and storing that in a new variable called “list
“.
Since we have used the “list
” as a variable name here, the list()
method will lose its properties and functionality and act like a normal variable.
We then declare a new variable called “car
“, and when we try to convert that into a list
by creating a list, we get TypeError: ‘list’ object is not callable error message.
The reason for TypeError is straightforward we have a list variable that is not a built function anymore as we re-assigned the built-in name list in the script. This means you can no longer use the predefined list value, which is a class object representing the Python list.
Solution for using the built-in name list as a variable name
If you are getting object is not callable error, that means you are simply using the built-in name as a variable in your code.
fruit = "Apple"
fruit_list = list(fruit)
print(fruit_list)
car="Ford"
car_list=list(car)
print(car_list)
Output
['A', 'p', 'p', 'l', 'e']
['F', 'o', 'r', 'd']
In our above code, the fix is simple we need to rename the variable “list” to “fruit_list”, as shown below, which will fix the ‘list’ object is not callable error.
Scenario 2 – Indexing list using parenthesis()
Another common cause for this error is if you are attempting to index a list of elements using parenthesis() instead of square brackets []. The elements of a list are accessed using the square brackets with index number to get that particular element.
Let us take a look at a simple example to reproduce this scenario.
my_list = [1, 2, 3, 4, 5, 6]
first_element= my_list(0)
print(" The first element in the list is", first_element)
Output
Traceback (most recent call last):
File "c:\Personal\IJS\Code\tempCodeRunnerFile.py", line 2, in <module>
first_element= my_list(0)
TypeError: 'list' object is not callable
In the above program, we have a “my_list
” list of numbers, and we are accessing the first element by indexing the list using parenthesis first_element= my_list(0)
, which is wrong. The Python interpreter will raise TypeError: ‘list’ object is not callable error.
Solution for Indexing list using parenthesis()
The correct way to index an element of the list is using square brackets. We can solve the ‘list’ object is not callable error by replacing the parenthesis ()
with square brackets []
to solve the error as shown below.
my_list = [1, 2, 3, 4, 5, 6]
first_element= my_list[0]
print(" The first element in the list is", first_element)
Output
The first element in the list is 1
Conclusion
The TypeError: ‘list’ object is not callable error is raised in two scenarios
- If you try to access elements of the list using parenthesis instead of square brackets
- If you try to use built-in names such as list as a variable name
Most developers make this common mistake while indexing the elements of the list or using the built-in names as variable names. PEP8 – the official Python style guide – includes many recommendations on naming variables properly, which can help beginners.
The post TypeError: ‘list’ object is not callable appeared first on ItsMyCode.
from Planet Python
via read more
No comments:
Post a Comment