How to Solve Python TypeError: ‘list’ object is not callable

by | Programming, Python, Tips

If you try to access items in a list using parentheses, you will raise the error: TypeError: ‘list’ object is not callable. We use parentheses to call a function in Python, but you cannot call a list.

This tutorial will go through the error in detail and an example scenario to learn how to solve it.

TypeError: ‘list’ object is not callable

What is a TypeError?

TypeError tells us that we are trying to perform an illegal operation for a specific Python data type. In this case, trying to call a Python list is not possible.

What is a Python List?

We can use lists to store multiple items in a single variable. You can create a list using square brackets. Let’s look at an example of a list:

pizzas = ["margherita", "four cheeses", "pepperoni", "ham and pineapple"]

To access items inside this list, you must specify the index number of the value you want to access inside square brackets. Let’s look at an example of accessing a list:

print(pizzas[0])
margherita

The code returns the first item in the list, “margherita”. If you try to access items in a list using parentheses (), you will raise the error: TypeError: ‘list’ object is not callable.

Example: Trying to Call a List

Let’s write a program that converts a list of strings to all lowercase. We can start by declaring the list of strings:

particles = ["ElECtroN", "muON", "PHoTOn", "neUTrinO"]

Next, we will create a for loop to iterate over the list of particle names and convert each name to lowercase using the lower() function.

for i in range(len(particles)):

   particles[i] = particles(i).lower()

   print(particles[i])

print(particles)

We use the range() function to iterate through every item in the particles list. We change the value of each name to all lowercase and print the updated variable to the console. Lastly, the program prints out the updated list to the console. Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
      1 for i in range(len(particles)):
      2     particles[i] = particles(i).lower()
      3     print(particles[i])
      4 

TypeError: 'list' object is not callable

We raise the error because we used parentheses to access the items in the list: particles(i).lower(). In Python, we use parentheses to call functions. Therefore, the Python interprets thinks we are trying to call a list, which is impossible.

Solution

To solve this error, we must use square brackets to access the items in the list. The revision will tell the Python interpreter we want to access the item at index position “i” in the list “particles”.

for i in range(len(particles)):

   particles[i] = particles[i].lower()

   print(particles[i])

print(particles)

Let’s run the code to see the effects of the revisions:

electron
muon
photon
neutrino

['electron', 'muon', 'photon', 'neutrino']

The code successfully returns the individual items and the complete list in lowercase.

Summary

Congratulations on reading to the end of this tutorial!

The error “TypeError: ‘module’ object is not callable” occurs when you try to access items in a list using parentheses. The Python interpreter thinks you are trying to call a list as a function, and calling a list is an illegal operation in Python. To correctly access items in a list, use square brackets and the index value for the item you want.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨