How to Fix: TypeError: ‘dict_keys’ object is not subscriptable

3D illustration of a claw hitting the glass of a display case, representing the dict_keys not subscriptable error.

This TypeError dict_keys error means you are trying to use square brackets [] on a dict_keys object, which isn’t allowed.

A dict_keys object is the special “view object” that a dictionary returns when you use the .keys() method.

Problem Code:

my_dict = {"name": "Alice", "age": 30}
keys = my_dict.keys()

print(keys) # Output: dict_keys(['name', 'age'])

# Now we try to get the first key
first_key = keys[0]
# CRASH! TypeError: 'dict_keys' object is not subscriptable

⚡ Quick Fix: TypeError: ‘dict_keys’ object is not subscriptable — Python list() Conversion Fix for .keys(), .values(), and .items() Index Access

You called .keys() on a dictionary and tried to access a position with [] — dict_keys is a live view object, not a list, and view objects have no index access.

# WRONG — dict_keys has no index, [] crashes immediately
my_dict = {"name": "Alice", "age": 30}
keys = my_dict.keys()
first_key = keys[0]          # TypeError: 'dict_keys' object is not subscriptable

# WRONG — same error fires on .values() and .items()
first_value = my_dict.values()[0]   # TypeError: 'dict_values' object is not subscriptable
first_pair  = my_dict.items()[0]    # TypeError: 'dict_items' object is not subscriptable

# RIGHT — convert to list first, then access by index
key_list   = list(my_dict.keys())
first_key  = key_list[0]            # Output: 'name'

# RIGHT — one-liner conversion for inline use
first_key  = list(my_dict.keys())[0]
first_val  = list(my_dict.values())[0]
first_pair = list(my_dict.items())[0]

# RIGHT — if you only need to loop, skip the conversion entirely
for key in my_dict.keys():          # dict_keys is iterable — no list() needed
    print(key)

The breakdown below explains what a view object is, why it exists, and when to convert versus when to loop directly.

Why it fails

A dict_keys object is a dynamic view of the dictionary’s keys, not a list.

It’s iterable (you can loop over it), but it’s not “subscriptable” (you can’t access items by index [0]).

The Fix: Convert it to a list

If you need to access keys by their position (like [0]), you must first convert the dict_keys object into a standard list.

my_dict = {"name": "Alice", "age": 30}
keys = my_dict.keys()

# THE FIX:
key_list = list(keys)

# Now it works!
first_key = key_list[0]
print(first_key) # Output: 'name'

This same error will also happen if you try this with .values() or .items(). The fix is the same: wrap them in list() to get a subscriptable copy.


TypeError: ‘dict_keys’ object is not subscriptable — View Objects, list() Conversion, and When to Skip Both

TypeError: ‘dict_keys’ object is not subscriptable fires because Python 3 redesigned .keys(), .values(), and .items() to return live view objects instead of static lists. The view reflects the current state of the dictionary — add a key, the view updates instantly. That live link is the trade-off for losing index access.

Three dict methods return view objects. All three fire this error on bracket access.

my_dict.keys() → dict_keys — iterable, no index
my_dict.values() → dict_values — iterable, no index
my_dict.items() → dict_items — iterable, no index

Convert with list() when you need index access, slicing, or any operation that requires a sequence. list(my_dict.keys())[0] grabs the first key. list(my_dict.items())[-1] grabs the last key-value pair. The conversion creates a static snapshot — changes to the dictionary after the conversion don’t appear in the list.

Skip the conversion when you only need to iterate. for key in my_dict.keys(): works without list() — view objects are fully iterable. For simple loops, dict iteration, and membership checks (if “name” in my_dict.keys():), the view object is faster and uses less memory than a converted list.

The most concise membership check skips .keys() entirely:

if “name” in my_dict: # faster than: if “name” in my_dict.keys()
print(my_dict[“name”])

Python checks dictionary keys directly on the dict object — .keys() is redundant for membership tests. Use it only when you need the view object itself or its list conversion.

Similar Posts

Leave a Reply