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.
Table of contents
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:
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.
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.
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!
Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!