[Solved] TypeError: List Object is Not Callable

In this article, we will be discussing the TypeError: “List” Object is not callable exception. We will also be through solutions to this problem with example programs.

Why Is this Error Raised?

This exception is raised when a list type is accessed as a function or the predefined term “list” is overwritten. The cause for these two situations are the following:

  1. list” being used as a variable name.
  2. Using parenthesis for list indexing

list” Being Used as a Variable Name

Variable declaration using in-built names or functions is a common mistake in rookie developers. An in-built name is a term with its value pre-defined by the language itself. That term can either be a method or an object of a class.

list() is an in-built Python function. As we discussed, it is not advisable to use pre-defined names as variable names. Although using a predefined name will not throw any exception in itself, the function under the name will no longer be accessible.

Let’s refer to an example.

website = "PythonPool"
list = list(website)
print(list)

content = "Python Material"
contentList = list(content)
print(contentList)

Output and Explanation

list object not callable
  1. The variable website consists of PythonPool
  2. The variable website is stored in the variable list as a list using list()
  3. We print the variable list, producing the required output.
  4. Similarly, another variable content stores “Python Material”
  5. We use list() and pass content as argument.
  6. Upon printing contentList, we get the mentioned error.

What went wrong here? In step 2, we store the list type in a variable called list, which is a predefined function. When were try to use the list function again in step 5, it fails. Python only remembers list as a variable since step 2. Therefore, list() has lost all functionality after being declared as a variable.

Solution

Instead of using list as a variable declaration, we can use more descriptive variable names that are not pre-defined (myList, my_list, nameList). For programming, follow PEP 8 naming conventions.

website = "PythonPool"
myList = list(website)
print(myList)

content = "Python Material"
contentList = list(content)
print(contentList)

Correct Output

['P', 'y', 't', 'h', 'o', 'n', 'P', 'o', 'o', 'l']
['P', 'y', 't', 'h', 'o', 'n', ' ', 'M', 'a', 't', 'e', 'r', 'i', 'a', 'l']

Using Parenthesis for List Indexing

Using parenthesis “()” instead of square brackets “[]” can also give rise to TypeError: List Object is not callable. Refer to the following demonstration:

myList = [2, 4, 6, 8, 10]
lastElement = myList(4)
print("the final element is: ", lastElement)

Output and Explanation

 Parenthesis for List Indexing Error
  1. Variable myList consists of a list of integers.
  2. We are accessing the last index element and storing it in lastElement.
  3. Finally, we are printing lastElement.

In line 2, we are accessing the final element of the list using parenthesis (). This is syntactically wrong. Indexing in Python lists requires that you pass the index values in square brackets.

Solution

Use square brackets in place of parenthesis.

myList = [2, 4, 6, 8, 10]
lastElement = myList[4]
print("the final element is: ", lastElement)

Correct Output

the final element is:  10

Python Error: “list” Object Not Callable with For Loop

def main():
     myAccounts=[]

     numbers=eval(input())

          ...
          ...
          ...
          ...

          while type!='#':
               ...
               ...
                 for i in range(len(myAccounts())): 
                        if(account==myAccounts[i].getbankaccount()):
                        index=i
                        ...
                        ...
main()

Output and Explanation

 Traceback(most recent call last):
....
....
     for i in range(len(myAccounts())):
     TypeError: 'list' object is not callable

The problem here is that in for i in range(len(myAccounts())): we have called myAccounts(). This is a function call. However, myAccounts is a list type.

Solution

Instead of calling myAccounts as a function, we will be calling it as a list variable.

def main():
     myAccounts=[]

     numbers=eval(input())

          ...
          ...
          ...
          ...

          while type!='#':
               ...
               ...
                 # for i in range(len(myAccounts)): 
                        if(account==myAccounts[i].getbankaccount()):
                        index=i
                        ...
                        ...
main()

TypeError:’ list’ object is Not Callable in Lambda

nums = [3,6,9,10,12]
list = list(nums)
print(list)

LambdaList = [1,2,3,4,5,6,7,8,9]
myList = list(filter(lambda a:(a%2==0), LamdaList))
print(myList)

Output and Explanation

 LambdaList = [1,2,3,4,5,6,7,8,9]
 myList = list(filter(lambda a:(a%2==0), list1))
 print(myList)

TypeError: 'list' object is not callable

list is used in line 2. This removes all functionality of the list function at line 5.

Solution

Avoid using pre-defined variable names.

nums = [3,6,9,10,12]
numList = list(nums)
print(numList)

LambdaList = [1,2,3,4,5,6,7,8,9]
myList = list(filter(lambda a:(a%2==0), LamdaList))
print(myList)

wb.sheetnames() TypeError: ‘list’ Object Is Not Callable

import openpyxl

mySheet = openpyxl.load_workbook("Sample.xlsx")
names = mySheet.sheetnames

print(names)
print(type(mySheet))

Output and Explanation

TypeError: 'list' object is not callable

In line 3, we have called mySheet.sheetnames. To get the name of all the sheets, do:

import openpyxl

mySheet = openpyxl.load_workbook("Sample.xlsx")
names = mySheet.sheet_names()

print(names)
print(type(mySheet))

# to access specific sheets
mySheet.get_sheet_by_name(name = 'Sheet 1') 

TypeError: ‘list’ Object is Not Callable in Flask

server.py

@server.route('/devices',methods = ['GET'])
def status(): 
    return app.stat()

if __name__ == '__main__':
        app.run()

app.py

def stat():
    return(glob.glob("/myPort/2203") + glob.glob("/alsoMyPort/3302"))

test.py

url = "http://1278.3.3.1:1000"

response = requests.get(url + " ").text
print(response)

Output and Explanation

"TypeError": 'list' object is not callable.

The final result is a list. In flask, the only two valid return types are strings or response types (JSON).

Solution

Make sure the return type is a string.

@server.route('/myDevices')
def status():
    return ','.join(app.devicesStats())

FAQs

What is a TypeError?

Python has several standard exceptions, including TypeError. When an operation is performed on an incorrect object type, a TypeError is raised.

How do we check if the object is a list before using its method?

In order to check if it is a list object, we can pass the isinstance() function like so:
if isinstance(myList, list):
  print("valid list)
else:
    print("invalid list")

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments