How to Fix TypeError: ‘List’ Object Is Not Callable in Python

TypeError: ‘list’ object is not callable is an error in Python that’s caused when a list is thrown as if it was a function. Here’s how to fix it.

Written by Reza Lavarian
Published on Jul. 11, 2024
two developers solving a coding error on a monitor
Image: Shutterstock / Built In
Brand Studio Logo

The “TypeError: ‘list’ object is not callable” error occurs when you try to call a list (list object) as if it was a function.

Here’s what the error looks like:

Traceback (most recent call last):
  File "/dwd/sandbox/test.py", line 6, in 
    more_items = list(range(11, 20))
                 ^^^^^^^^^^^^^^^^^^^
TypeError: 'list' object is not callable

Calling a list object as if it’s a callable isn’t something you’d do on purpose, though. It usually occurs when you’re using the wrong syntax or overriding a function name with a list object.

TypeError: ‘List’ Object Is Not Callable Explained

Python “TyperError: ‘list’ object is not callable” occurs when a list is called as if it was a function. Some of the most common causes include declaring a variable with a name that’s also the name of the function, indexing a list with parenthesis, calling a method with the same name as a property and calling a method decorated with @property.  

Let’s explore the common causes and their solutions.

 

What Causes the TypeError: ‘List’ Object Is Not Callable?

This TypeError happens under various scenarios:

  1. Declaring a variable with a name that’s also the name of a function.
  2. Indexing a list by parenthesis rather than square brackets.
  3. Calling a method that’s also the name of a property.
  4. Calling a method decorated with @property.

Declaring a variable with a name that's also the name of a function: A Python function is an object like any other built-in object, such as str, int, float, dict, and list, etc.

All built-in functions are defined in the builtins module and assigned a global name for easier access. For instance, list refers to the __builtins__.list function.

That said, overriding a function (accidentally or on purpose) with any value (such as a list object) is technically possible.

In the following example, we’ve declared a variable named list containing a list of numbers. In its following line, we try to create another list, this time by using the list() and range() functions:

list = [1, 2, 3, 4, 5, 6, 8, 9, 10] 
# ⚠️ list is no longer pointing to the list function

# Next, we try to generate a sequence to add to the current list
more_items = list(range(11, 20))
# 👆 ⛔ Raises: TypeError: ‘list’ object is not callable

If you run the above code, Python will produce a “TypeError: ‘list’ object is not callable” error because we’ve already assigned the list name to the first list object.

More on PythonUnderstanding Duck Typing in Python

 

How to Fix TypeError: ‘List’ Object Is Not Callable

There are two ways to fix the issue:

  1. Rename the variable list.
  2. Explicitly access the list() function from the builtins module (__bultins__.list).

The second approach isn’t recommended unless you’re developing a module. For instance, if you want to implement an open() function that wraps the built-in open():

# Custom open() function using the built-in open() internally
def open(filename):
     # ...
     __builtins__.open(filename, 'w', opener=opener)
     # ...

In almost every other case, you should always avoid naming your variables as existing functions and methods. But if you’ve done so, renaming the variable would solve the issue.

So the above example could be fixed like this:

items = [1, 2, 3, 4, 5, 6, 8, 9, 10] 

# Next, we try to generate a sequence to add to the current list
more_items = list(range(11, 20))

This issue is common with function names you’re more likely to use as variable names. Functions such as vars, locals, list, all or even user-defined functions.

In the following example, we declare a variable named all containing a list of items. At some point, we call all() to check if all the elements in the list (also named all) are True:

all = [1, 3, 4, True, 'hey there', 1]
# ⚠️ all is no longer pointing to the built-in function all()


# Checking if every element in all is True:
print(all(all))
# 👆 ⛔ Raises TypeError: 'list' object is not callable

Obviously, we get the TypeError because the built-in function all() is now shadowed by the new value of the all variable.

To fix the issue, we choose a different name for our variable:

items = [1, 3, 4, True, 'hey there', 1]


# Checking if every element in all is True:
print(all(items))
# Output: True

Long story short, you should never use a function name (built-in or user-defined) for your variables.

A tutorial on how to resolve the TypeError: ‘list’ object is not callable error. | Video: 5-Minute Python

More on PythonTiming Function in Python: A Guide

 

TypeError: ‘List’ Object Is Not Callable Examples and Solutions

Overriding functions (and calling them later on) is the most common cause of the “TypeError: ‘list’ object is not callable” error. It’s similar to calling integer numbers as if they’re callables. Now, let’s get to the less common mistakes that lead to this error.

1. Indexing a List by Parenthesis

Indexing a list by parenthesis rather than square brackets: Another common mistake is when you index a list by () instead of []. Based on Python semantics, the interpreter will see any identifier followed by a () as a function call. And since the parenthesis follows a list object, it’s like you’re trying to call a list.

As a result, you'll get the “TypeError: ‘list’ object is not callable” error.

items = [1, 2, 3, 4, 5, 6]

print(items(2))
# 👆 ⛔ Raises TypeError: 'list' object is not callable

This is how you’re supposed to access a list item:

items = [1, 2, 3, 4, 5, 6]

print(items[2])
# Output: 3

2. Calling a Method That’s the Name of a Property

When you define a property in a class constructor, it’ll shadow any other attribute of the same name.

class Book:
    def __init__(self, title, authors):
        self.title = title
        self.authors = authors

    def authors(self):
        return self.authors

book = Book('The Pragmatic Programmer', ['David Thomas', 'Andrew Hunt'])
print(book.authors())
# 👆 ⛔ Raises TypeError: 'list' object is not callable

Since we have a property named authors, the method authors() is shadowed. As a result, any reference to authors will return the property authors, returning a list object. And if you call this list object value like a function, you’ll get the “TypeError: ‘list’ object is not callable” error.

The name get_authors sounds like a safer and more readable alternative:

class Book:
    def __init__(self, title, authors):
        self.title = title
        self.authors = authors

    def get_authors(self):
        return self.authors

book = Book('The Pragmatic Programmer', ['David Thomas', 'Andrew Hunt'])
print(book.get_authors())
# Output: ['David Thomas', 'Andrew Hunt']

3. Calling a Method With @Property Decorator

The @property decorator turns a method into a “getter” for a read-only attribute of the same name. You need to access a getter method without parentheses, otherwise you’ll get a TypeError.

class Book:
    def __init__(self, title, authors):
        self._title = title
        self._authors = authors

    @property
    def authors(self):
        """Get the authors' names"""
        return self._authors

book = Book('The Pragmatic Programmer', ['David Thomas', 'Andrew Hunt'])
print(book.authors())
# 👆 ⛔ Raises TypeError: 'list' object is not callable

To fix it, you need to access the getter method without the parentheses:

book = Book('The Pragmatic Programmer', ['David Thomas', 'Andrew Hunt'])
print(book.authors)
# Output: ['David Thomas', 'Andrew Hunt']

Problem solved!

Frequently Asked Questions

The “TypeError: ‘list’ object is not callable” error occurs when you try to call a list (list object) as if it was a function. The following scenarios can trigger it:

  1. Declaring a variable with a name that’'s also the name of a function.
  2. Indexing a list by parenthesis rather than square brackets.
  3. Calling a method that's also the name of a property.
  4. Calling a method decorated with @property.

There are two ways to fix the issue:

  1. Rename the variable list.
  2. Explicitly access the list() function from the builtins module (__bultins__.list).
Explore Job Matches .