Python Error: TypeError: 'tuple' object is not callable Solution

Posted in /  

Python Error: TypeError: 'tuple' object is not callable Solution
vinaykhatri

Vinay Khatri
Last updated on November 24, 2024

    A tuple is an immutable data structure in Python . It is very similar to the Python list and stores the elements in sequential order using indexing. The only difference between a Python list and a Python tuple is that the tuple is immutable and uses parenthesis to initialize elements. However, the tuple uses parenthesis () to store the elements, still in value accessing it accept index value inside the square bracket [] .

    If we try to access a tuple element using parathesis instead of the square bracket, we will encounter the " TypeError: 'tuple' object is not callable " Error.

    In this Python Error guide solution, we will walk through TypeError: 'tuple' object is not callable Error and discusses why this error occurs in Python and how to debug it. We will also discuss a common example when you may commit a mistake and encounter this error in your program. So without further ado, let's get started with the error statement.

    Python Error: TypeError: 'tuple' object is not callable

    The error statement TypeError: 'tuple' object is not callable is divided into two parts separated with a colon." : ".

    1. Python Exception Type ( TypeError )
    2. Error Message ( 'tuple'object is not callable )

    1. TypeError

    TypeError is a standard Python exception. It is raised in a Python program when we apply an inappropriate or unsupported function or operation on a python object.

    2. 'tuple'object is not callable

    This is the error message, which is telling us that the tuple object we are trying to access is not callable. In Python, the call statement is related to a function call, and in a function call, we use the function name followed by the parenthesis. For example function_name() . Whenever the Python interpreter reads the statement where it finds a variable name followed by the parenthesis, it calls that variable as a function.

    But if that variable is not a function object, the Python interpreter returns a TypeError followed by a message that the following object is not callable. This same thing happens when we put parenthesis after a tuple object. Python interpreter tries to call that object as a function, but when it finds out that the object is not a function but a tuple, it returns the Error Message: 'tuple'object is not callable .

    Example

    # initialize a tuple
    my_tuple = (1,2,3,4,5,6)
    
    print('First Element: ',my_tuple(0))

    Output

    Traceback (most recent call last):
      File "main.py", line 4, in 
        print('First Element: ',my_tuple(0))
    TypeError: 'tuple' object is not callable

    Break the code

    In this example, we are receiving the error in line 4 with print('First Element: ',my_tuple(0)) statement. If we look closely at the code, we are trying to print the first element from our tuple object my_tuple using the statement my_tuple(0) .  The problem with this statement is we are using parenthesis () , but it should be a square bracket [] . Python treated the my_tuple(0) statement as a function calling statement, but soon it realized that my_tuple is no function but a tuple object, and it threw the error.

    Solution

    The solution to the above example is straightforward. All we need to do is use the square bracket instead of parenthesis when we try to access the tuple element using indexing.

    # initialize a tuple
    my_tuple = (1,2,3,4,5,6)
    
    print('First Element: ',my_tuple[0])

    Output

    First Element: 1

    Common Example Scenario

    There are two common mistakes when many Python developers encounter this error.

    1. Forget to put commas between two tuple objects.
    2. Use parenthesis instead of the square bracket to access a tuple element (as we have discussed in the above example)

    Forget to put commas between two tuple objects

    Let's say you are assigning two tuple objects to two variables using tuple unpacking or trying to put multiple tuple elements in a tuple or list object and forget to put commas between them to separate. There, you will encounter this error.

    Example 1 (forget to put a comma between two tuple values in tuple unpacking)

    tuple1, tuple2 = (1,2,3,4) (5,6,7,8)   #error
    
    print("Tuple 1:", tuple1)
    print("Tuple 2:" , tuple2)

    Output

    Traceback (most recent call last):
    File "main.py", line 1, in <module>
    tuple1, tuple2 = (1,2,3,4) (5,6,7,8)
    TypeError: 'tuple' object is not callable

    In the above example, we are getting this error because there is no comma between the (1,2,3,4) and (5,6,7,8) statements. And the object (1,2,3,4,5) is a tuple, and python treats them (5,6,7,8) as a function call with arguments. This led to an error of tuple calling.

    Example 1 Solution

    The debug the above example we need to put the comma between the two tuple values.

    tuple1, tuple2 = (1,2,3,4) , (5,6,7,8) #solved
    
    print("Tuple 1:", tuple1)
    print("Tuple 2:" , tuple2)

    Output

    Tuple 1: (1, 2, 3, 4)
    Tuple 2: (5, 6, 7, 8)

    Example 2 (Forget to put a comma between two tuple values, inside a list)

    # list
    students = [
                    ('Rahul', 19, 993),
                    ('Ravi', 19, 987)
                    ('Jay', 19, 980)
    ]
    
    for i,student in enumerate(students):
        print(f"Rank {i+1}:" , student[0], 'Age:',student[1], 'Score', student[2])

    Output

    Traceback (most recent call last):
    File "main.py", line 4, in <module>
    ('Ravi', 19, 987)
    TypeError: 'tuple' object is not callable

    In this example, we are getting this error because we forget to put a comma between ('Ravi', 19, 987) and ('Jay', 19, 980) .

    Example 2 Solution

    # list
    students = [
                ('Rahul', 19, 993),
                ('Ravi', 19, 987), #solved
                ('Jay', 19, 980)
                ]
    
    for i,student in enumerate(students):
        print(f"Rank {i+1}:" , student[0], 'Age:',student[1], 'Score', student[2])

    Output

    Rank 1: Rahul Age: 19 Score 993
    Rank 2: Ravi Age: 19 Score 987
    Rank 3: Jay Age: 19 Score 980

    Conclusion

    Now let's wrap up our Python TypeError: ‘tuple’ object is not callable Solution article. In this article, we learned why the TypeError: ‘tuple’ object is not callable error occurs in Python and how to solve it. This error occurs when we put parenthesis after a tuple object or value.

    To solve this problem, we need to make sure we are using square brackets instead of parenthesis during tuple element access. And there should always be a comma between two tuple values. If you are still encountering this error in your Python program, please share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments