Tuples are enclosed within parentheses. This can be confusing because function calls also use parenthesis. If you use parentheses to access items from a tuple, or if you forget to separate tuples with a comma, you’ll encounter a “TypeError: ‘tuple’ object is not callable” error.
In this guide, we talk about what this error means and what causes it. We walk through two examples to help you understand how you can solve this error in your code.
TypeError: ‘tuple’ object is not callable
Tuples are defined as a list of values that are enclosed within parentheses:
coffees = ("Macchiato", "Americano", "Latte")
Tuple objects are accessed in the same way as a list item. Indexing syntax lets you retrieve an individual item from a tuple. Items in a tuple cannot be accessed using parenthesis.
There are two potential causes for the “TypeError: ‘tuple’ object is not callable” error:
- Defining a list of tuples without separating each tuple with a comma
- Using the wrong indexing syntax
Let’s walk through each cause individually.
Cause #1: Missing Comma
The “TypeError: ‘tuple’ object is not callable” error is sometimes caused by one of the most innocent mistakes you can make: a missing comma.
Define a tuple that stores information about a list of coffees sold at a coffee shop:
coffees = [ ("Americano", 72, 1.90), ("Macchiato", 93, 2.10) ("Latte", 127, 2.30) ]
The first value in each tuple is the name of a coffee. The second value is how many were sold yesterday at the cafe. The third value is the price of the coffee.
Now, let’s print “coffees” to the console so we can see its values in our Python shell:
Our code returns:
Traceback (most recent call last): File "main.py", line 3, in <module> ("Macchiato", 93, 2.10) TypeError: 'tuple' object is not callable
As we expected, an error is returned. This is because we have forgotten to separate all the tuples in our list of coffees with a comma.
When Python sees a set of parenthesis that follows a value, it treats the value as a function to call. In this case, our program sees:
("Macchiato", 93, 2.10)("Latte", 127, 2.30)
Our program tries to call (“Macchiato”, 93, 2.10) as a function. This is not possible and so our code returns an error.
To solve this problem, we need to make sure that all the values in our list of tuples are separated using commas:
coffees = [ ("Americano", 72, 1.90), ("Macchiato", 93, 2.10), ("Latte", 127, 2.30) ] print(coffees)
We’ve added a comma after the tuple that stores information on the Macchiato coffee. Let’s try to run our code again:
[('Americano', 72, 1.9), ('Macchiato', 93, 2.1), ('Latte', 127, 2.3)]
Our code successfully prints out our list of tuples.
Cause #2: Incorrect Indexing Syntax
Here, we write a program that stores information on coffees sold at a coffee shop. Our program will then print out each piece of information about each type of coffee beverage.
Start by defining a list of coffees which are stored in tuples:
coffees = [ ("Americano", 72, 1.90), ("Macchiato", 93, 2.10), ("Latte", 127, 2.30) ]
Next, write a for loop that displays this information on the console:
for c in coffees: print("Coffee Name: " + str(c(0))) print("Sold Yesterday: " + str(c(1))) print("Price: $" + str(c(2))))
This for loop should print out each value from all the tuples in the “coffees” list. We convert each value to a string so that we can concatenate them to the labels in our print() statements.
Run our code and see what happens:
Traceback (most recent call last): File "main.py", line 8, in <module> print("Coffee Name: " + c(0)) TypeError: 'tuple' object is not callable
Our code returns an error.
This error is caused because we are trying to access each item from our tuple using curly brackets. While tuples are defined using curly brackets, their contents are made accessible using traditional indexing syntax.
To solve this problem, we have to use square brackets to retrieve values from our tuples:
for c in coffees: print("Coffee Name: " + str(c)) print("Sold Yesterday: " + str(c)) print("Price: $" + str(c))
Let’s execute our code with this new syntax:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Coffee Name: Americano Sold Yesterday: 72 Price: $1.9 Coffee Name: Macchiato Sold Yesterday: 93 Price: $2.1 Coffee Name: Latte Sold Yesterday: 127 Price: $2.3
Our code successfully prints out information about each coffee.
The “TypeError: ‘tuple’ object is not callable” error is raised when you try to call a tuple as a function. This can happen if you use the wrong syntax to access an item from a tuple or if you forget to separate two tuples with a comma.
Make sure when you access items from a tuple you use square brackets. Also ensure that all tuples in your code that appear in a list are separated with a comma.
Now you’re ready to solve this error like a Python pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.