TypeError: ‘list’ object is not callable in Python

A list in Python is used to store various elements/items in a variable. While working with lists, you may encounter a “TypeError: list object is not callable” for various reasons.  Such as, the stated error occurs when you try to access a list incorrectly, overriding a list, accessing a list as a function, etc. The TypeError can be resolved via different approaches.

This write-up will provide you with a reason, and solution for TypeError “list object is not callable” in Python with numerous examples. The following aspects will be discussed in this write-up one by one:

So, let’s get started!

Reason 1: Incorrect Accessing of List

The first possible reason for the TypeError is when the list is wrongly accessed in the program. This means that the list is assessed using parentheses.

An example of this error is shown in the below code snippet.

Code:

In the above code, the error arises at the time of accessing when the parentheses are used instead of brackets inside the print() function.

Output:

The above output shows the “TypeError”.

Solution: Access the List Correctly

To remove this error, square brackets must be placed in the place of parentheses. The following code given below shows the solution:

Code:

List_Value = ['Alex', 'Blake', 'Candy']

print(List_Value[0])

In the above code, the error is corrected using the bracket instead of parentheses for accessing the list element.

Output:

The above output shows that the list element has been accessed successfully.

.

Reason 2: Overriding list() Function

Another possibility of the “TypeError” is when the “list()” function is overridden while initializing the list variable. An example of list overriding is shown in the below code snippet.

Code:

In the above code, the list is overridden inside the print() function.

Output:

The above output shows the “TypeError”.

Solution: Rename List Variable

To remove the stated error, the list variable must be renamed. After renaming, re-execute the program, and the error will be gone. Have a look at the following snippet below:

Code:

new_List = ['Alex', 'Blake', 'Candy']

print(list([1, 2, 3]))

In the above code, the first list variable name has been renamed and after that new list will be executed and printed on the screen without any error.

Output:

The above output shows the list values.

Reason 3: Calling List as a Function

This error also occurs when the list variable is called like a function. The below code shows how the stated error occurs when we call a list as a function.

Code:

In the above code, the list is called as a function “new_list()” i.e. without using brackets or any integer value inside it.

Output:

The above output shows the “TypeError”.

Solution: Removing the Parentheses From the List

To remove this error, we have to remove the parentheses or use the square bracket with index value to access any element of the list. The below code shows the solution to this error:

Code:

new_List = ['Alex', 'Blake', 'Candy']

print(new_List)

In the above code, the parentheses are removed from the list variable inside the print() function.

Output:

The above output shows the value of the list.

Reason 4: Same Function Name and Variable

The “TypeError: list object is not callable” occurs when the function and list variable are initialized in a Python script with the same name.

Code:

The function and the list variable in the above code are initialized with the same name, “value”.

Output:

The above output shows the “TypeError”.

Solution: Rename List Variable/Function

To remove the error, change the name of the function or the name of the list variable. The program below shows the solution to the error.

Code:

def value():
    return 'its-linux-foss'

value_1 = ['Alex', 'Blake', 'Candy']

print(value())

In the above code, the name of the list variable is changed from “value” to “value_1”.

Output:

The above output shows the value of the function.

That’s it from this guide!

Conclusion

The “TypeError: ‘list’ object is not callable” occurs in Python due to incorrect accessing of the list, calling list as a function, overriding list variable, same function, and list variable name. To access a specific value of the list, use a square bracket and pass the desired index number within the square bracket. To resolve the stated error, access the list appropriately, don’t override the list, don’t access the list as a function, etc. This article presented a detailed guide on how to fix the type error “list object is not callable” in Python.