TypeError: 'list' object is not callable in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Jan 31, 2023
8 min

banner

# Table of Contents

  1. TypeError: 'list' object is not callable in Python
  2. TypeError: 'set' object is not callable in Python
  3. TypeError: 'generator' object is not callable in Python

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

The Python "TypeError: 'list' object is not callable" occurs when we try to call a list as a function using parentheses ().

To solve the error, use square brackets when accessing a list at a specific index, e.g. my_list[0].

typeerror list object is not callable

Here is one example of how the error occurs.

main.py
my_list = ['bobby', 'hadz', 'com'] # โ›”๏ธ TypeError: 'list' object is not callable print(my_list(0)) # ๐Ÿ‘ˆ๏ธ uses parenthesis instead of square brackets

The issue is that we used parenthesis instead of square brackets when accessing the list at a specific index.

# Use square brackets to access an item in a list

Instead, use square brackets when accessing the list at an index.

main.py
my_list = ['bobby', 'hadz', 'com'] print(my_list[0]) # ๐Ÿ‘‰๏ธ "bobby" print(my_list[1]) # ๐Ÿ‘‰๏ธ "hadz"

use square brackets to access item in list

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(a_list) - 1.

You should also use square brackets when getting a slice of a list.

main.py
my_list = ['bobby', 'hadz', 'com'] print(my_list[0:2]) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz'] print(my_list[1:3]) # ๐Ÿ‘‰๏ธ ['hadz', 'com']

use square brackets to access slice of list

The syntax for list slicing is a_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

If the start index is omitted, it is considered to be 0, if the stop index is omitted, the slice goes to the end of the list.

# Common causes of the error

The error occurs for multiple reasons:

  • Trying to access a list with parentheses () instead of square brackets [].
  • Having a function and a variable with the same name.
  • Overriding a built-in function by mistake and setting it to a list.
  • Having a class method and a class property with the same name.
  • Calling a function that returns a list twice.

# Make sure you aren't overriding the list() function

Another thing to look out for is that you're not overriding the built-in list() function.

main.py
# ๐Ÿ‘‡๏ธ overriding built-in function list = ['bobby', 'hadz', 'com'] # โ›”๏ธ TypeError: 'list' object is not callable print(list([1, 2, 3]))

To solve the error in this situation, rename your variable and restart your script.

main.py
# โœ… not overriding built-in list anymore my_list = ['bobby', 'hadz', 'com'] print(list([1, 2, 3]))

The two variables now have different names, so the issue is resolved.

# Trying to call a list as a function causes the error

Another common cause of the error is simply trying to call a list as a function.

main.py
my_list = ['a', 'b', 'c'] # โ›”๏ธ TypeError: 'list' object is not callable print(my_list())

To solve the error, you either have to remove the parentheses or figure out how the variable got assigned a list instead of a function or a class.

main.py
my_list = ['bobby', 'hadz', 'com'] print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

We removed the parentheses to print the value of the variable.

# Having a class method and a class property with the same name

The error is also caused when we have a class method and a class property with the same name.

main.py
class Employee(): def __init__(self, tasks): # ๐Ÿ‘‡๏ธ this attribute hides the method self.tasks = tasks # ๐Ÿ‘‡๏ธ same name as class variable def tasks(self): return self.tasks emp = Employee(['dev', 'test']) # โ›”๏ธ TypeError: 'list' object is not callable print(emp.tasks())

The Employee class has a method and an attribute with the same name.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the object is not callable error.

To solve the error, you have to rename the class method.

main.py
class Employee(): def __init__(self, tasks): self.tasks = tasks def get_tasks(self): return self.tasks emp = Employee(['dev', 'test']) print(emp.get_tasks()) # ๐Ÿ‘‰๏ธ ['dev', 'test']

Once you rename the method, you will be able to call it without any issues.

# Having a function and a variable with the same name

Make sure you don't have a function and a variable with the same name.

main.py
def example(): return 'bobbyhadz.com' # ๐Ÿ‘‡๏ธ shadowing function example = ['a', 'b', 'c'] # โ›”๏ธ TypeError: 'list' object is not callable print(example())

The example variable shadows the function with the same name, so when we try to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

main.py
def example(): return 'bobbyhadz.com' my_list = ['a', 'b', 'c'] print(example()) # ๐Ÿ‘‰๏ธ bobbyhadz.com print(my_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c']

The variable and the function are now named differently, so the issue is resolved.

# Calling a function that returns a list twice

Here is another example of how the error occurs.

main.py
def example(): return ['bobby', 'hadz', 'com'] # โ›”๏ธ TypeError: 'list' object is not callable example()()

Notice that we used two sets of parentheses.

The first set invoked the example function. The function returns a list, so the second set of parentheses invoked the list object.

To solve the error, remove the second set of parentheses.

main.py
def example(): return ['bobby', 'hadz', 'com'] print(example()) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

Now we only call the function once and print the result.

# Conclusion

To solve the "TypeError: 'list' object is not callable" error, make sure:

  • You aren't trying to access a list with parentheses () instead of square brackets [].
  • You don't have a function and a variable with the same name.
  • You aren't overriding a built-in function and setting it to a list.
  • You don't have a class method and a property with the same name.
  • You aren't calling a function that returns a list twice.

# Table of Contents

  1. TypeError: 'set' object is not callable in Python
  2. TypeError: 'generator' object is not callable in Python

# TypeError: 'set' object is not callable in Python

The Python "TypeError: 'set' object is not callable" occurs when we try to call a set object as a function, e.g. by overriding the built-in set() function.

To solve the error, make sure you aren't overriding set() and resolve any clashes between function and variable names.

typeerror set object is not callable

# Overriding the built-in set() function

Here is one example of how the error occurs.

main.py
# ๐Ÿ‘‡๏ธ overriding built-in set() set = set(['a', 'b', 'c']) # โ›”๏ธ TypeError: 'set' object is not callable set(['d', 'e', 'f'])

We override the built-in set() function, setting it to a set object and try to call it as a function.

You can solve the error by renaming your variable.

main.py
# โœ… Not overriding built-in set() anymore my_set = set(['a', 'b', 'c']) print(set(['d', 'e', 'f'])) # ๐Ÿ‘‰๏ธ {'d', 'f', 'e'}

We gave the variable a different name, so it no longer clashes with the built-in set() function.

# Trying to call a set as a function

Another common cause of the error is simply trying to call a set as a function.

main.py
my_set = {'a', 'b', 'c'} # โ›”๏ธ TypeError: 'set' object is not callable my_set()

To solve the error, either remove the parentheses or figure out how the variable got assigned a set instead of a function or a class.

main.py
my_set = {'a', 'b', 'c'} print(my_set) # ๐Ÿ‘‰๏ธ {'a', 'c', 'b'}

Removing the parentheses resolved the issue and we were able to print the set.

If the variable is supposed to point to a function or method, track down where it got assigned a set and correct the assignment.

# Having a function and a variable with the same name

Make sure you don't have a function and a variable with the same name.

main.py
def example(): return 'bobbyhadz.com' example = {'a', 'b', 'c'} # โ›”๏ธ TypeError: 'set' object is not callable print(example())
The example variable shadows the function with the same name, so when we try to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

main.py
def example(): return 'bobbyhadz.com' a_set = {'a', 'b', 'c'} print(example()) # ๐Ÿ‘‰๏ธ bobbyhadz.com

We gave the variable a different name, so it no longer clashes with the function.

Now we are able to call the function without any issues.

# Having a class method and a class property with the same name

The error is also caused when we have a class method and a class property with the same name.

main.py
class Employee(): def __init__(self, tasks): # ๐Ÿ‘‡๏ธ this attribute hides the method self.tasks = tasks # ๐Ÿ‘‡๏ธ same name as class variable def tasks(self): return self.tasks emp = Employee({'dev', 'test'}) # โ›”๏ธ TypeError: 'set' object is not callable print(emp.tasks())

The Employee class has a method and an attribute with the same name.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the 'set' is not callable error.

To solve the error, you have to rename the class method.

main.py
class Employee(): def __init__(self, tasks): self.tasks = tasks def get_tasks(self): return self.tasks emp = Employee({'dev', 'test'}) print(emp.get_tasks()) # ๐Ÿ‘‰๏ธ {'test', 'dev'}

We renamed the method, so it no longer clashes with the class property.

Now, we can call the method on an instance of the class without any issues.

# TypeError: 'generator' object is not callable in Python

The Python "TypeError: 'generator' object is not callable" occurs when we try to call a generator object as if it were a function.

To solve the error, remove the parentheses and make sure you don't have any clashes between function and variable names.

typeerror generator object is not callable

Here is an example of how the error occurs

main.py
def g(): yield 1 yield 2 yield 3 gen = g() print(type(gen)) # ๐Ÿ‘‰๏ธ <class 'generator'> # โ›”๏ธ TypeError: 'generator' object is not callable print(gen())

We tried to call a generator object as a function which caused the error.

# Remove the parentheses to solve the error

To solve the error, remove the parentheses.

main.py
def g(): yield 1 yield 2 yield 3 gen = g() # ๐Ÿ‘ˆ๏ธ creates generator for i in gen: print(i) # ๐Ÿ‘‰๏ธ 1, 2, 3

We use parentheses when creating a generator. However, you don't have to use parentheses when iterating over one in a for loop.

Make sure you don't have a second set of parentheses because the first set creates a generator object and the second set calls the generator object, causing the error.

# Use the next() function to get the next item

Alternatively, you can pass the generator to the next() function to retrieve the next item.

main.py
def g(): yield 1 yield 2 yield 3 gen = g() print(next(gen)) # ๐Ÿ‘‰๏ธ 1 print(next(gen)) # ๐Ÿ‘‰๏ธ 2 print(next(gen)) # ๐Ÿ‘‰๏ธ 3

Make sure you don't have an extra set of parentheses when creating the generator.

main.py
def g(): yield 1 yield 2 yield 3 # โ›”๏ธ TypeError: 'generator' object is not callable gen = g()() # ๐Ÿ‘ˆ๏ธ remove extra set of parentheses

# Having a function and a variable with the same name

Another common cause of the error is having a clash between variable and function names.

main.py
def g(): yield 1 yield 2 yield 3 def gen(): return 'bobbyhadz.com' gen = g() # โ›”๏ธ TypeError: 'generator' object is not callable gen()

The gen variable shadows the gen() function, so when we try to call the function, we end up calling the variable.

To solve the error, rename the variable or the function.

main.py
def g(): yield 1 yield 2 yield 3 def greet(): return 'bobbyhadz.com' gen = g() print(greet()) # ๐Ÿ‘‰๏ธ "bobbyhadz.com"
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev