Python List Object is Not Callable [Reasons and Solutions Explained]
Table of Contents
Table of Contents
What is python list object is not callable error?
list object is not callable error is a type error which occurs when we try to perform any unsupported operation on the list object .
This error is raised by python interpreter when the objects / datatypes that you are working are not compatible.
Accessing of List using wrong Symbol
When you use parentheses instead of square brackets to access the elements of the list the “Python list object is not callable” error is raised.
For Example:
numbers = [10, 20 ,30 ,40, 50]
print(numbers(1))
Output:
Traceback (most recent call last):File "/home/main.py", line 3, in <module>print(numbers(1))
TypeError: 'list' object is not callable
To avoid this error use Square brackets to access elements of list
numbers = [10, 20 ,30 ,40, 50]
print(numbers[1])
Output:
20
Using variable name as list
when you declare the variable with the variable name as “list”, you will override the original python list function which will result in loosing functionality of list() function and raise an type error.
For example:
list = ["John", "Mike","David" ,"Michel"]
list2 = list(range(0,5))
for item in list2:
print(item)
Output:
File "/home/main.py", line 2, in <module>list2 = list(range(0,5))
TypeError: 'list' object is not callable
To avoid this error always use variable names other than the python built in functions
list1 = ["John", "Mike","David" ,"Michel"]
list2 = list(range(0,5))
for item in list1:
print(item)
Output:
John
Mike
David
Michel
Using a list object as a function parameter
When we pass the list variable as the function to any method it will raise list object is not callable error.
users = ["John", "Mike","David" ,"Michel"]print(users())
Output:
Traceback (most recent call last):File "/home/main.py", line 3, in <module>print(users())
TypeError: 'list' object is not callable
Using Same Function Name and Variable
This error occurs when user declares the list variable same as the user defined method doing this causes ambiguity in the python interpreter and list object is not callable error is raised.
def users():
print("This is users functions")
users = ["John", "Mike","David" ,"Michel"]
users()
Output:
Traceback (most recent call last):File "/home/main.py", line 7, in <module>users()
TypeError: 'list' object is not callable
If we declare the function after the list variable then we will be only able to access the function and print(user) will generate Name error.
users = ["John", "Mike","David" ,"Michel"]
def users():
print("This is users functions")
users()
Output:
This is users functions