. Advertisement .
..3..
. Advertisement .
..4..
The TypeError: ‘list’ object is not callable in Python is among one the errors that you can land in. Beginner programmers are aware of the Type Errors as these are something that pops up when they code a program. To have a Type error is not a hard nut to crack as there is no such error that can’t be solved.
When you are calling a list object in Python, you can see the TypeError: ‘list’ object is not callable warning message. The reason for this error is the use of the parenthesis ‘()’.
Now, let’s shed light on how to get through this error
How to Fix TypeError: ‘list’ object is not callable
As we have discussed above, the prime reason of this error is to call a function ‘list’ using the parenthesis ‘()’. Have a look at how this error message come:
Code:
list1 = ['Apple', 'Pear', 'Orange']
print(list1(0))
Output:
Traceback (most recent call last):
File "code.py", line 2, in <module>
print(list1(0))
TypeError: 'list' object is not callable
Using Square Brackets
The use of parenthesis ‘()’ causes the TypeError: ‘list’ object is not callable in Python, so replacing it with square brackets can solve the issue.
list1 = ['Apple', 'Pear', 'Orange']
print(list1[0]) # Apple
print(list1[1]) # Pear
print(list1[2]) # Orange
Check the list() Function
The next thing that you need to check is the list()
function. Make sure it doesn’t override the built-in list()
.
Code:
list = ['Singapore', 'Brazil', 'France']
print(list([1, 2, 3]))
Output:
Traceback (most recent call last):
File "code.py", line 2, in <module>
print(list(['x', 'y', 'z']))
TypeError: 'list' object is not callable
Solution
To solve the error, after renaming the variable, restart the script.
list2 = ['Singapore', 'Brazil', 'France']
print(list(['x', 'y', 'z'])) # ['x', 'y', 'z']
Check how a function is called
To call a list as a function is also one of the reasons that can cause the error.
Code:
list3 = ['Swan', 'Falcon', 'Sparrow']
print(list3())
Output:
Traceback (most recent call last):
File "code.py", line 2, in <module>
print(list3())
TypeError: 'list' object is not callable
This can be solved in two ways; remove the parenthesis or find out how a list is assigned to the variable instead of a class or a function.
list3 = ['Swan', 'Falcon', 'Sparrow']
print(list3) # ['Swan', 'Falcon', 'Sparrow']
Use a different name for class property and method
Another reason to have the TypeError is that you name property and method the same.
Code:
class Position():
def __init__(self, tasks):
self.tasks = tasks # this attribute hides the method
def tasks(self): # same name as class variable
return self.tasks
pos = Position(['Founder', 'Manager', 'Chief'])
print(pos.tasks())
Output:
Traceback (most recent call last):
File "code.py", line 8, in <module>
print(pos.tasks())
TypeError: 'list' object is not callable
Solution
Just rename the class method, like this:
class Position():
def __init__(self, tasks):
self.tasks = tasks
def get_tasks(self):
return self.tasks
pos = Position(['Founder', 'Manager', 'Chief'])
print(pos.get_tasks()) # ['Founder', 'Manager', 'Chief']
Use a different name for a Function and Variable
Having the same name of a function and a variable can occur the error. In this way, the variable can shadow the function.
Code:
def sample():
return 'TypeError: list object is not callable'
sample = ['Germany', 'France', 'England']
print(sample())
Output:
Traceback (most recent call last):
File "code.py", line 5, in <module>
print(sample())
TypeError: 'list' object is not callable
After renaming the variable or the function can solve the error.
def sample_str():
return 'TypeError: list object is not callable'
sample_country = ['Germany', 'France', 'England']
print(sample_str()) # TypeError: list object is not callable
print(sample_country) # ['Germany', 'France', 'England']
Conclusion
All the above-mentioned methods can help you solve the TypeError: ‘list’ object is not callable in Python.
I wish you happy coding!
Leave a comment