Typeerror: dict_values object is not subscriptable [SOLVED]

In this article, we will show you how to fix the Python error “typeerror: dict_values object is not subscriptable.”

The “dict_values object is not subscriptable” is a Python error that is usually encountered by developers when they attempt to access an item from a dictionary view object using indexing, which isn’t allowed in Python.

TypeError (What is typeerror?)

Typeerror is a common error in Python that arises when an operation or function is applied to a value of an improper type. This error indicates that the data type of an object isn’t compatible with the operation or function that is being used.

Python (What is Python?)

Python is one of the most popular programming languages. It is used for developing a wide range of applications.

In addition to that, Python is a high-level programming language that is usually used by developers nowadays due to its flexibility.

Typeerror: dict_values object is not subscriptable – SOLUTION

Fixing the error “typeerror: dict_values object is not subscriptable” may sound difficult, but don’t worry, we’ll assist you with this one.

Here’s a sample code that can raise the error:

s_dict = {'sx': 90, 'sy': 99, 'sz': 92}
val_view = s_dict.values()
print(val_view[0])

Error:

Traceback (most recent call last):
File "C:\Users\pies-pc1\PycharmProjects\pythonProject\main.py", line 3, in
print(val_view[0])
~~~~^^^
TypeError: 'dict_values' object is not subscriptable

Solution

To solve this error, try to convert the dictionary view object to a list, as it supports indexing.

Example Code:

s_dict = {'sx': 90, 'sy': 99, 'sz': 92}
val_view = s_dict.values()
val_list = list(val_view)
print(val_list[0])

This will print the value of 1.

Output:

90

Alternative Solution

Aside from the solution provided above, you can also try this alternative solution: Use for loop. Instead of converting the dictionary view object to a list or tuple, use a for loop.

Example code:

s_dict = {'sx': 90, 'sy': 99, 'sz': 92}
val_view = s_dict.values()
for value in val_view:
print(value)

Output:

90
99
92

Tips to avoid getting Typeerrors

The following are some tips to avoid getting type errors in Python.

  1. Avoid using the built-in data types in Python in the wrong way.

    → Make certain that your variables and data structures are using the correct data types.

  1. Always check or confirm the types of your variables.

    → To check the types of your variables, use the type() function. This will allow you to confirm if the type of your variable is appropriate.

  1. Be clear and concise when writing code.

    → Being clear and concise when writing your code can help you avoid typeerrors. It is because it will become easier to understand.

  1. Handle the error by using try-except blocks.

    → Try using the try-except blocks to catch and handle any typeerror that may arise when working with code.

  1. Use the built-in functions of Python if needed.

    → Use built-in functions such as int(), str(), float(), or bool() if you need to convert a variable to a different type.

Understanding “object is not subscriptable”

Subscript access (obj[key]) requires __getitem__ on the object. Integers, floats, booleans, functions, and None do not implement it. When you write x[0] on any of these, TypeError fires.

Common triggers

  • Indexing an int. If you meant to convert to string: str(num)[0].
  • Indexing None. Usually a function returned None where a dict was expected.
  • Calling instead of indexing. If my_dict = {} and later you overwrite with my_dict = some_function, then my_dict["key"] fails.
  • Using set with subscript. Sets have no order — my_set[0] is not allowed.
  • Nested access on a shallow structure. data["a"]["b"] fails when data[“a”] is None or a scalar.

Diagnostic pattern

# BAD
def parse_config(text):
    if not text:
        return  # implicit None
    return json.loads(text)

config = parse_config("")
db_host = config["db"]["host"]  # TypeError: 'NoneType' object is not subscriptable

# GOOD — return empty dict, then defensive check
def parse_config(text):
    if not text:
        return {}
    return json.loads(text)

config = parse_config("")
db_host = config.get("db", {}).get("host", "localhost")

Best practices

  • Use dict.get() with defaults. Chains cleanly for nested lookups.
  • Return empty containers, not None, from functions that produce dicts or lists.
  • Use pathlib or dict-utils for deep-key access on dynamic data.

Frequently Asked Questions

What is Python TypeError and what causes it?

TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.

How do I quickly debug a Python TypeError?

Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.

Should I catch TypeError or let it propagate?

For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.

How do I prevent TypeError in production?

Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.

Where can I find more TypeError fixes?

Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.

Conclusion

In conclusion, the error “typeerror: dict_values object is not subscriptable” in Python can be easily solved by either converting the dictionary view object to a list or using a for loop.

I think that’s all for this article, ITSourceCoders! I hope you’ve learned a lot from this. If you have any questions, please leave a comment below.

For more typeerror tutorials, visit our website. Thank you for reading!

Elijah Galero


Programmer & Technical Writer at PIES IT Solution

Elijah Galero is a programmer and writer at PIES IT Solution, author of 175+ tutorials at itsourcecode.com. Specializes in Python error debugging (AttributeError, TypeError, ModuleNotFoundError), Python programming tutorials, and Microsoft Excel how-to guides for BSIT students and productivity learners.

Expertise: Python · Python Errors · Python AttributeError · Python TypeError · ModuleNotFoundError · MS Excel · MS PowerPoint
 · View all posts by Elijah Galero →

Leave a Comment