“type” is a special keyword in Python that denotes a value whose type is a data type. If you try to access a value from an object whose data type is “type”, you’ll encounter the “TypeError: ‘type’ object is not subscriptable” error.
This guide discusses what this error means and why you may see it. It walks you through an example of this error so you can learn how to fix the error whenever it comes up.
TypeError: ‘type’ object is not subscriptable
Python supports a range of data types. These data types are used to store values with different attributes. The integer data type, for instance, stores whole numbers. The string data type represents an individual or set of characters.
Each data type has a “type” object. This object lets you convert values to a particular data type, or create a new value with a particular data type. These “type” objects include:
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
If you check the “type” of these variables, you’ll see they are “type” objects:
The result of this code is: “type”.
We cannot access values from a “type” object because they do not store any values. They are a reference for a particular type of data.
An Example Scenario
Build a program that displays information about a purchase made at a computer hardware store so that a receipt can be printed out. Start by defining a list with information about a purchase:
purchase = type(["Steelseries", "Rival 600 Gaming Mouse", 69.99, True])
The values in this list represent, in order:
- The brand of the item a customer has purchased
- The name of the item
- The price of the item
- Whether the customer is a member of the store’s loyalty card program
Next, use print() statements to display information about this purchase to the console:
print("Brand: " + purchase) print("Product Name: " + purchase) print("Price: $" + str(purchase))
You print the brand, product name, and price values to the console. You have added labels to these values so that it is easy for the user to tell what each value represents.
Convert purchase to a string using
str() because this value is stored as a floating point number and you can only concatenate strings to other strings.
Next, check to see if a user is a member of the store’s loyalty card program. You do this because if a customer is not a member then they should be asked if they would like to join the loyalty card program:
if purchase == False: print("Would you like to join our loyalty card program?") else: print("Thanks for being a member of our loyalty card program. You have earned 10 points for making a purchase at our store.")
If a user is not a member of the loyalty card program, the “if” statement runs. Otherwise, the
else statement runs and the user is thanked for making a purchase.
Run our code and see if it works:
Traceback (most recent call last): File "main.py", line 3, in <module> print("Brand: " + purchase) TypeError: 'type' object is not subscriptable
Our code returns an error.
Take a look at the offending line of code:
print("Brand: " + purchase)
The “subscriptable” message says you are trying to access a value using indexing from an object as if it were a sequence object, like a string, a list, or a tuple. In the code, you’re trying to access a value using indexing from a “type” object. This is not allowed.
This error has occurred because you’ve defined the “purchase” list as a type object instead of as a list. To solve this error, remove the “type” from around our list:
purchase = ["Steelseries", "Rival 600 Gaming Mouse", 69.99, True]
There is no need to use “type” to declare a list. You only need to use “type” to check the value of an object. Run our code and see what happens:
Brand: Steelseries Product Name: Rival 600 Gaming Mouse Price: $69.99 Thanks for being a member of our loyalty card program. You have earned 10 points for making a purchase at our store.
The code prints out the information about the purchase. It also informs that the customer is a loyalty card member and so they have earned points for making a purchase at the store.
The “TypeError: ‘type’ object is not subscriptable” error is raised when you try to access an object using indexing whose data type is “type”. To solve this error, ensure you only try to access iterable objects, like tuples and strings, using indexing.
Now you’re ready to solve this error like a Python expert!
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.