Python Error "TypeError: 'type' object is not subscriptable" Solution

Posted in /  

Python Error "TypeError: 'type' object is not subscriptable" Solution
vinaykhatri

Vinay Khatri
Last updated on November 22, 2024

    type is a reserved keyword in Python. If we print the type keyword, we get an object by name <class type> . We can also pass a data object to the type() function [ type(data_object)] and get the data type of the object. If we treat a value returned by the type() function as a list object and try to perform indexing on that value, we will encounter TypeError: 'type' object is not subscriptable .

    This Python tutorial will discuss this error in detail and learn how to solve it. We will also walk through a typical example to get an idea of where you may encounter this error.

    So without further ado, let's get started!

    Python Error TypeError: 'type' object is not subscriptable

    We know that Python has different data types that store values with different attributes. For instance, the integer type stores whole numbers, and the string type stores a set of characters.

    Further, each data type has the 'type' object, which lets you convert values into a specific data type or create a new value with a particular type. The following are the 'type' objects in Python:

    • int()
    • str()
    • tuple()
    • dict()

    When you check the type of these variables, they are 'type' objects.

    print(type(int))

    Output

    <class 'type'>

    TypeError: 'type' object is not subscriptable is a standard Python error, and like other error statements, it is divided into two parts.

    1. Exception Type ( TypeError )
    2. Error Message ( 'type' object is not subscriptable )

    1. TypeError

    TypeError is a standard Python exception type. It occurs when we perform an invalid operation on a Python data type object. Performing an addition or concatenation operation between an integer value and a string value is a common Python TypeError exception error.

    2. 'type' object is not subscriptable

    Python has three standard subscriptable objects: list, tuple, and string. All these three objects support indexing, which allows us to perform the square bracket notation to access the individual elements or characters from these data-type objects.

    Example

    # python string
    string = "Hello"
    # python tuple
    tuple_ = (1,2,3,4)
    # python list
    list_= [1,2,3,4]
    
    # acessing string tuple and list with indexing
    
    print(string[0])      #H
    print(tuple_[1])      #2
    print(list_[2])       #3

    But, if we perform the indexing notation on a value that is returned by the type() function, we receive the Error Message ' type' object is not subscriptable .

    This error message states that we perform a subscriptable notation like indexing on the 'type' object, and the 'type' object does not support indexing or subscriptable .

    Error Example

    name ="Rahul"
    
    #data type of name
    name_dt = type(name)       #<class 'str'>
    
    # performing indexing on type object
    print(name_dt[0])

    Output

    Traceback (most recent call last):
    File "main.py", line 7, in <module>
    print(name_dt[0])
    TypeError: 'type' object is not subscriptable

    Break the code

    In this example, we received the error at line 7 because we performed indexing on name_dt variable whose value is <class 'str'> and its data type is <class 'type'> .  When we perform an indexing operation on a 'type' object, we receive the TypeError: 'type' object is not subscriptable error.

    Python "TypeError: 'type' object is not subscriptable" - Common Scenario

    Many new programmers encounter this error when they use the same name to store the string value and the data type of string returned by the type() function.

    Example

    # string
    name = "Rahul"
    
    # data type of name
    name = type(name)
    
    print("The Data type of name is: ", name)
    print('The first character of name is: ', name[0])

    Output

    The Data type of name is: <class 'str'>
    
    Traceback (most recent call last):
    File "main.py", line 8, in <module>
    print('The first character of name is: ', name[0])
    TypeError: 'type' object is not subscriptable

    Break the code

    In this example, we encountered the error in line 8 with print('The first chracter of name is: ', name[0]) statement. This is because, in line 5, we changed the value of name to <class 'str'> by assigning type(name) statement.

    After that statement, the value of the name became <class 'str'> and its type became <class 'type'> . And when we try to access the first letter of the value 'Rahul' using the name[0] statement, we got the error.

    Solution

    The solution to the above problem is straightforward. All we need to do is provide the different variable names to the type(name) value.

    # string
    name = "Rahul"
    
    # data type of name
    name_dt = type(name)
    
    print("The Data type of name is:", name_dt)
    print('The first character of name is: ', name[0])

    Output

    The Data type of name is: <class 'str'>
    The first character of name is: R

    Conclusion

    This was all about the TypeError: 'type' object is not subscriptable error, which is a very common error. We can easily debug this error if we understand its cause. It occurs when we perform the indexing operation on the Python type object. To resolve this error, we must ensure we are not using indexing on the type object.

    If you still get this error in your Python program, please share your code in the comment section. We will try to help you with debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments