Online Tutorials & Training Materials | STechies.com
Register Login

TypeError: 'list' object is not callable in Python

26 Nov 2019 1:31 pm || 0

If you are working with lists in Python, a very common operation is accessing an element from the list. You can do this by using the index of the required element. In that case, the name of the list and the index has to be specified. 

For example, print (name_list[2]) will print the third element of the list called name_list. You need to use square brackets to mention the index number. But in case you use parenthesis for indexes, you will encounter an error “TypeError 'list' object is not callable in Python”.

We will look at the way to fix this error.       

Error: TypeError 'list' object is not callable in Python

This is a common programming error in Python which makes every programmer as a novice programmer. This type of error occurs when you try to access an element of a list using parenthesis “()”.

As we all know that python take parenthesis “()” to run a function, but when you try to access the value of list using parenthesis “()“ instead of using brackets  “ [] “ then python compiler generates the Error: “TypeError: 'list' object is not callable”

TypeError 'list' object is not callable in Python

Example:

# Inilised a list
list1 = ['Hi', 'This', 'is','STechies']

# Inilised variable i as 0
i = 0

# Inilised empty string
string1 = ''

# Run While loop to list length
while i < len(list1):
  # Joint each value of list to final string
  string1 = string1 + ' ' + list1(i)
  i += 1

# Print final Output
print(string1)

Output:

Traceback (most recent call last):
  File "str.py", line 7, in <module>
    string1 = string1 + ' ' + list1(i)
TypeError: 'list' object is not callable

In the above example, we are trying to access an element of a list using parenthesis “list1(i)” instead of brackets “list1[i]”

Due to which Python compiler try to run list1(i) as a function and generates an error: “TypeError: 'list' object is not callable”

How to resolve TypeError: 'list' object is not callable

To resolve this error, you need to use brackets “list1[i]" instead of parenthesis “list1(i)” to access an element of the list as shown in the example below:  

Correct Example:

# Initialised a list
list1 = ['Hi', 'This', 'is','STechies']

# Inilised variable i as 0
i = 0

# Inilised empty string
string1 = ''

# Run While loop to list length
while i < len(list1):
  # Joint each value of list to final string
  string1 = string1 + ' ' + list1[i]
  i += 1

# Print final Output
print(string1)

Output:

Hi This is STechies