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

by | Programming, Python, Tips

If you try to call a tuple object, you will raise the error “TypeError: ‘tuple’ object is not callable”.

We use parentheses to define tuples, but if you define multiple tuples without separating them with commas, Python will interpret this as attempting to call a tuple.

To solve this error, ensure you separate tuples with commas and that you index tuples using the indexing operator [] not parentheses ().

This tutorial will go through how to solve this error with the help of code examples.


TypeError: ‘tuple’ object is not callable

What is a TypeError?

TypeError occurs in Python when you perform an illegal operation for a specific data type.

What Does Callable Mean?

Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name(). Let’s look at an example of a working function that returns a string.

# Declare function

def simple_function():

    print("Hello World!")

# Call function

simple_function()
Hello World!

We declare a function called simple_function in the code, which prints a string. We can then call the function, and the Python interpreter executes the code inside simple_function().

We use tuples to store multiple items in a single variable. Tuples do not respond to a function call because they are not functions. If you try to call a tuple, the Python interpreter will raise the error TypeError: ‘tuple’ object is not callable. Let’s look at examples of raising the error and how to solve it:

Example #1: Not Using a Comma to Separate Tuples

Let’s look at an example where we define a list of tuples. Each tuple contains three strings. We will attempt to print the contents of each tuple as a string using the join() method.

# Define list of tuples

lst = [("spinach", "broccolli", "asparagus"),
 
       ("apple", "pear", "strawberry")

       ("rice", "maize", "corn")
]

# Print types of food

print(f"Vegetables: {' '.join(lst[0])}")

print(f"Fruits: {' '.join(lst[1])}")

print(f"Grains: {' '.join(lst[2])}")

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [1], in <cell line: 3>()
      1 # Define list of tuples
      3 lst = [("spinach", "broccolli", "asparagus"),
      4  
----> 5        ("apple", "pear", "strawberry")
      6 
      7        ("rice", "maize", "corn")
      8 ]
     10 # Print types of food
     12 print(f"Vegetables: {' '.join(lst[0])}")

TypeError: 'tuple' object is not callable

We get the TypeError because we do not have a comma separating the second and third tuple item in the list. The Python Interpreter sees this as an attempt to call the second tuple with the contents of the third tuple as arguments.

Solution

To solve this error, we need to place a comma after the second tuple. Let’s look at the revised code:

# Define list of tuples

lst = [("spinach", "broccolli", "asparagus"),
       ("apple", "pear", "strawberry"),
       ("rice", "maize", "corn")
]

# Print types of food

print(f"Vegetables: {' '.join(lst[0])}")

print(f"Fruits: {' '.join(lst[1])}")

print(f"Grains: {' '.join(lst[2])}")

Let’s run the code to get the correct output:

Vegetables: spinach broccolli asparagus
Fruits: apple pear strawberry
Grains: rice maize corn

Example #2: Incorrectly Indexing a Tuple

Let’s look at an example where we have a tuple containing the names of three vegetables. We want to print each name by indexing the tuple.

# Define tuple

veg_tuple = ("spinach", "broccolli", "asparagus")

print(f"First vegetable in tuple: {veg_tuple(0)}")

print(f"Second vegetable in tuple: {veg_tuple(1)}")

print(f"Third vegetable in tuple: {veg_tuple(2)}")

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
      1 veg_tuple = ("spinach", "broccolli", "asparagus")
      2 
----≻ 3 print(f"First vegetable in tuple: {veg_tuple(0)}")
      4 print(f"Second vegetable in tuple: {veg_tuple(1)}")
      5 print(f"Third vegetable in tuple: {veg_tuple(2)}")

TypeError: 'tuple' object is not callable

The error occurs because we are using parentheses to index the tuple instead of the indexing operator []. The Python interpreter sees this as calling the tuple passing an integer argument.

Solution

To solve this error, we need to replace the parenthesis with square brackets. Let’s look at the revised code:

# Define tuple

veg_tuple = ("spinach", "broccolli", "asparagus")

print(f"First vegetable in tuple: {veg_tuple[0]}")

print(f"Second vegetable in tuple: {veg_tuple[1]}")

print(f"Third vegetable in tuple: {veg_tuple[2]}")

Let’s run the code to get the correct output:

First vegetable in tuple: spinach
Second vegetable in tuple: broccolli
Third vegetable in tuple: asparagus

Summary

Congratulations on reading to the end of this tutorial. To summarize, TypeError’ tuple’ object is not callable occurs when you try to call a tuple as if it were a function. To solve this error, ensure when you are defining multiple tuples in a container like a list that you use commas to separate them. Also, if you want to index a tuple, use the indexing operator [] , and not parentheses.

For further reading on not callable TypeErrors, go to the article: How to Solve Python TypeError: ‘float’ object is not callable.

To learn more about Python, specifically for data science and machine learning, go to the online courses page on Python.

Have fun and happy researching!

Attribution

If you found this guide helpful, feel free to link back to this post for attribution and share it with others!

Feel free to use these formats to reference our tools in your articles, blogs, or websites.

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 ✨