TryBugHunt

TypeError: 'list' object is not callable

TypeError: 'list' object is not callable in Python means you put ( ) after something that is not a function. Usually a built-in name like list or str has been overwritten by your own variable.

PythonPattern: Scope and closure bugs

How do I fix TypeError: 'list' object is not callable?

TypeError: 'list' object is not callable means you put ( ) after a list. Usually you named a variable list, which hides the built-in list() function, or you wrote items(0) instead of items[0]. Rename the variable, for example to numbers, and use square brackets for indexing. In a notebook, restart or run del list.

  1. 1. Search for list =Search the file for list = or other built-in names used as variables, like str, sum or max.
  2. 2. Check bracket typeitems(0) calls the list. Indexing needs square brackets: items[0].
  3. 3. Restart the notebookIn Jupyter or the REPL, the shadowed name stays until you restart the kernel or run del list.

What does TypeError: 'list' object is not callable mean?

Calling means ( ). Python is telling you the thing before the parentheses is a list (or str, int, dict) — data, not a function. The same message appears as 'str' object is not callable, 'int' object is not callable and so on; the type in quotes is what the name actually holds right now.

Why does TypeError: 'list' object is not callable happen?

1. Shadowing a built-in

Naming a variable list, str, sum or max replaces the built-in for the rest of that scope. The crash comes later, where you call the built-in.

Breaks

list = [3, 1, 2]
unique = list(set(items))

Works

numbers = [3, 1, 2]
unique = list(set(items))

2. Parentheses instead of square brackets

items(0) calls items. items[0] indexes it.

Breaks

first = items(0)

Works

first = items[0]

3. An attribute with the same name as a method

Assigning self.total = 0 in __init__ hides a method called total().

Breaks

class Cart:
    def __init__(self):
        self.total = 0
    def total(self):
        return sum(self.prices)

Works

class Cart:
    def __init__(self):
        self.running_total = 0
    def total(self):
        return sum(self.prices)

How to find it in your own code

Search your file for `name =` where name is the thing being called. If you shadowed a built-in, rename your variable. In an interactive session or notebook, the shadowing survives until you restart — `del list` restores the built-in.

Try it: fix this bug

easy

class_report([40, 60, 80]) crashes: TypeError: 'int' object is not callable.

def class_report(scores):
    sum = 0
    for s in scores:
        sum += s
    passed = sum(1 for s in scores if s >= 50)
    return [sum, passed]
Open with hintsRuns in your browser. No account needed.

More challenges with this error

Each of these challenges runs into this error. Find the bug and fix it, no install and no account needed.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.