typeerror: ‘list’ object is not callable
is a Python error, and it is not that common, but still, you should know why this error occurs in Python and how to solve it. This error generally occurs when we use
()
parenthesis to access Python list elements instead of square brackets. Python interpreter treats parenthesis like a function call. That's why it throws this error.
In this Python guide, we will discuss this error in detail and learn how to solve it with the help of some examples.
The Problem: typeerror: ‘list’ object is not callable
Whenever you encounter this error during executing your Python program, you just need to know that you are trying to access a list element using parenthesis
()
instead of a square bracket
[]
. The error statement
typeerror: ‘list’ object is not callable
is divided into two parts.
1.
typeerror
:
It is a kind of error present in Python. And it generally represents those kinds of errors in which we try to access the property or functionality of an object using the invalid property.
2.
‘list’ object is not callable
:
Statement specifies the error message. It tells us that the list object is not callable. In Python, callable objects are functions and methods. And to call them, we use the function name or method name followed by the parenthesis. But if we apply the same syntax to the Python list, we get the type error with this error message.
Error Reason
Python list is a data container that stores its elements in sequential order. To access the individual elements from the list, we use the element index value inside the square bracket and proceed by list variable name.
Example
>>> my_list = ['a', 'b', 'c', 'd']
>>> #access list element using []
>>> print(my_list[2])
c
But if we use
()
parenthesis to access the list element instead of square brackets
[]
, the Python interpreter will throw the
TypeError: 'list' object is not callable
Error.
>>> my_list = ['a', 'b', 'c', 'd']
>>> #access list element using ()
>>> print(my_list(2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Why?
The variable name followed by the parenthesis and a value inside it is the syntax for the function call.
Example
def hello(number):
print(number)
# function call
hello(20)
So when we pass the parenthesis along with the Python list, the interpreter treats the statement as function calls, and the function call is not valid for a list, so Python throws the
TypeError: 'list' object is not callable
Error. This simply means we are confusing the list object with a function object.
Example
hello = ['Hi', 'Hey', 'greetings', 'bonjour']
# calling funciton
hello(3)
Output
Traceback (most recent call last):
File "main.py", line 4, in <module>
hello(3)
TypeError: 'list' object is not callable
The common Scenario
Errors are nothing but the small mistakes we commit during writing code. And it is difficult to write a complete project without committing any errors. The most common case when we commit this error is when we are looping throw the list using its index value and performing any function on the element at the same time.
Let's create an example where we have a list of lowercase alphabets, and we need to convert them into uppercase alphabets.
Example
# list of lowercase alphabet
alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for i in range(len(alpha)):
# print lowercase letter to uppercase
alpha[i] = alpha(i).upper()
print(alpha)
Output
Traceback (most recent call last):
File "main.py", line 6, in <module>
alpha[i] = alpha(i).upper()
TypeError: 'list' object is not callable
Solution
In our above program, we are receiving the error at
line 6
where we are converting the lowercase letter to uppercase using
alpha[i] = alpha(i).upper()
statement.
In that statement, we are trying to access the
alpha
list element using
()
brackets, which is wrong. To solve the problem, we just need to use
[]
brackets instead of
()
brackets to access the list elements.
Solution
# list of lowercase alphabet
alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for i in range(len(alpha)):
# print lowercase letter to uppercase
alpha[i] = alpha[i].upper()
print(alpha)
Output
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
Conclusion
The Python error
‘list’ object is not callable
, is a
typeerror
that occurs when the Python interpreter mistreats the list element access as a function call. This happens because we mistakenly use the
()
bracket to access the list element that is supposed to be a square bracket
[]
. Indexing always supports a square bracket, and for the function calls, we use the () bracket with the variable name.
If you are still getting this error in your python program, please comment down your code. We will help you to debug it.
People are also reading:
- Gmail API in Python
- Python typeerror: ‘str’ object is not callable Solution
- Delete Emails in Python
- Python TypeError: ‘float’ object cannot be interpreted as an integer Solution
- Python Numpy Array Tutorial
- What is a constructor in Python?
- Numpy dot Product
- How to Play sounds in Python?
- Python Check If File or Directory Exists
- Read File in Python
Leave a Comment on this Post