Last modified: Apr 23, 2026 By Alexander Williams

Fix TypeError: 'type' object is not subscriptable

Python is a powerful and beginner-friendly language. But sometimes it throws confusing errors. One common error is TypeError: 'type' object is not subscriptable. This error stops your code from running. It usually happens when you try to use square brackets [] on a type object like list, dict, or str instead of an instance of that type. Understanding this error is key to writing clean Python code. This article will explain what causes it and how to fix it.

What Does This Error Mean?

The error message says: TypeError: 'type' object is not subscriptable. In Python, "subscriptable" means an object supports square bracket indexing. For example, you can use my_list[0] to get the first item. But if you try list[0], Python gets confused. list is a type (a class), not an instance. Types are not subscriptable by default. You must create an instance first.

Let’s see a simple example that triggers this error.


# Example 1: Wrong usage of a type
my_list = list[1, 2, 3]  # This line causes the error
print(my_list)

Output:
TypeError: 'type' object is not subscriptable

Here, list[1, 2, 3] tries to use square brackets on the list type itself. The correct way is list([1, 2, 3]) or simply [1, 2, 3].

Common Causes of This Error

This error has a few common causes. Beginners often make these mistakes. Let’s explore them with code examples.

1. Using Square Brackets Instead of Parentheses

The most frequent cause is using [] when you should use () to call a constructor. For instance, creating a list, dictionary, or set with wrong syntax.


# Wrong: uses square brackets on the type
my_dict = dict["name": "Alice"]  # Error

# Correct: use parentheses
my_dict = dict(name="Alice")
print(my_dict)  # Output: {'name': 'Alice'}

Remember: dict() is a function call. dict[...] is invalid.

2. Confusing a Type with an Instance

Sometimes you accidentally treat a class name as a variable. For example, if you assign a value to a variable named list, you shadow the built-in type. But the error appears when you try to index the type directly.


# Confusing: using list as a type with indexing
data = list[0]  # Error: 'type' object is not subscriptable

# Correct: first create a list instance
my_list = [10, 20, 30]
data = my_list[0]
print(data)  # Output: 10

3. Using Type Hints Incorrectly (Python 3.8 and earlier)

In older Python versions, you could not use built-in types as generics directly. For example, list[int] was not allowed. This caused the same error.


# Python 3.8 or earlier: this causes error
def process(items: list[int]) -> None:
    pass

# Fix: import from typing
from typing import List
def process(items: List[int]) -> None:
    pass

Since Python 3.9, you can use list[int] directly. But in older versions, you need the typing module. This is a common pitfall.

How to Fix the Error Step by Step

Here are practical steps to resolve this error. Follow them when you encounter this issue.

Step 1: Check Your Syntax

Look at the line where the error occurs. Are you using square brackets on a type name like list, dict, str, or int? Replace [] with () if you meant to create an instance.


# Bad
result = str[123]

# Good
result = str(123)
print(result)  # Output: '123'

Step 2: Verify You Are Not Shadowing Built-ins

Make sure you didn’t accidentally name a variable list, dict, or type. If you did, rename it. This avoids confusion.


# Bad: variable named list shadows the type
list = [1, 2, 3]
new_list = list[4]  # No error here, but confusing

# Better: use a different name
my_list = [1, 2, 3]

Step 3: Use Correct Type Hints

If you are writing type hints, ensure your Python version supports subscripting generics. For Python 3.8 or earlier, import from typing. For Python 3.9+, built-in types work.


# Python 3.9+ (works fine)
def get_first(items: list[str]) -> str:
    return items[0]

# Python 3.8 or earlier (use typing)
from typing import List
def get_first(items: List[str]) -> str:
    return items[0]

Real-World Example: Debugging a Program

Let’s fix a real program that has this error. Imagine you are building a simple contact book.


# Buggy code
contacts = dict["Alice": "123", "Bob": "456"]  # Error here
print(contacts["Alice"])

Output:
TypeError: 'type' object is not subscriptable

The fix is simple. Use parentheses and keyword arguments.


# Fixed code
contacts = dict(Alice="123", Bob="456")
print(contacts["Alice"])  # Output: 123

Alternatively, use literal syntax.


contacts = {"Alice": "123", "Bob": "456"}
print(contacts["Alice"])  # Output: 123

Always prefer literal syntax for simple cases. It is cleaner and avoids this error entirely.

Preventing the Error in the Future

To avoid this error, follow these best practices. They will save you debugging time.

  • Always use parentheses to call constructors: list(), dict(), str().
  • Use literal syntax when possible: [] for lists, {} for dicts.
  • Keep your Python version updated. Python 3.9+ has better type hint support.
  • Read error messages carefully. The line number tells you exactly where the problem is.

If you want to learn more about common Python errors, check out our guide on Python TypeError: Causes and Fixes. It covers many similar issues.

Conclusion

The TypeError: 'type' object is not subscriptable error is easy to fix once you understand it. It happens when you misuse square brackets on a type instead of an instance. Always use parentheses to create objects. Use literal syntax for lists and dicts. Check your type hints for Python version compatibility. With these tips, you can write error-free code. Keep practicing and debugging. Python will become second nature.