</bugz loading .
"List object is not callable" is one of the most common errors in python. It means that you are attempting to call a list ( a non callable object ) like a function.
In simple words, it basically means that you are trying to call a function which has a name of a list, in your code. Let us understand this with an example.
bugz = [1,2,3] # a list object
bugz() # calling a function which has the name of a list
TypeError: 'list' object is not callable
In the above example, the integer list bugz is being called like a function ( i.e. by adding parentheses () ), resulting in the above mentioned typeerror.
This unintentionally happens mostly in two situations, they are :
When a built-in function with the same name as your list exists.
When you attempt to access elements of the list by using parentheses rather than square brackets.
Regardless of the scope of the code, the Python interpreter provides more than 70 built-in functions and types ( that have predefined values ) which are always accessible. Python doesn't stop users from reassigning the built-in names, which means that all the built-in names can be used as the names of variables.
If you name a list object the same as a built-in function, the next time when you attempt to call the built-in function, python assumes that you are referring to the list object and not the built-in function.
Let us understand this with an example :
list = [1,2,3] # naming the list object "list"
# built-in list function being overrided as a list object
list2 = list("bugz") # referring to the list object as a function
print(list,list2)
TypeError: 'list' object is not callable
In the above example, the list object is being named as "list" ( same as a built-in function ), which results in the existing list function being overrided as a list object.
This means that Python now considers the list name to be referring to a list object rather than a function. Therefore, when the built-in list function is called in the next line, python assumes that you are calling the list object, and as you cannot call a non callable object ( list ) like a function, this typeerror occurs.
The solution for this typeerror is very simple. It is to update the name of the list object such that it differs from the names of the python built-in functions.
list1 = [1,2,3] # changing the list name to "list1"
# list function not being overrided
list2 = list("bugz") # calling the built-in list function
print(list1,list2)
[1, 2, 3] ['b', 'u', 'g', 'z']
In the above example, the name of the list instance is being changed to list1. Therefore, the built-in list function is not being overrided. Hence, it is very much recommended to not use built-in names of python ( such as list, map, range, etc. ) as variables.
While working with lists, accessing elements of lists is a common thing. In python, this can be done by specifying the index of the elements and the name of the list. For example; bugz[3] will return the element with the index number 3 from the list named bugz.
Usually people tend to use parentheses () instead of square brackets [] which results in the typeerror mentioned above.
bugz = ["zero","one","two"]
# using bugz() 👇 instead of bugz[]
print(bugz(1))
TypeError: 'list' object is not callable
In the above example, to access the element with index 1 from the list bugz, parentheses () are being used instead of square brackets [].
This makes python interpret it as a function call, and as bugz is a list ( a non callable object ) and you cannot call a list like a function, the error mentioned above occurs.
The simplest solution for this error is to access the elements from the list in the correct way, i.e. by using square brackets [] instead of parentheses (). Let us understand this with an example.
bugz = ["zero","one","two"]
print(bugz[0])
print(bugz[1])
print(bugz[2])
zero
one
two
In the above example, the use of parentheses () to access elements from the list is avoided.
TypeError: 'list' object is not callable error usually occurs when parentheses () are placed just after the name of a list. For example; list_name() results in the above mentioned TypeError. Try using advanced IDEs which help you in avoiding these type of errors.