Last modified: Apr 25, 2026 By Alexander Williams
Fix dict_keys Not Subscriptable Error
Have you seen the error TypeError: 'dict_keys' object is not subscriptable? It is a common Python mistake. This error happens when you try to use square brackets [] on a dict_keys object. In Python 3, dictionary methods like .keys() return a view object, not a list. You cannot index it directly.
This guide explains why the error occurs. You will learn how to fix it with simple code examples. We will also cover best practices to avoid it in the future. Let's dive in.
What Does This Error Mean?
In Python 2, .keys() returned a list. You could use my_dict.keys()[0] easily. In Python 3, this changed for efficiency. The method returns a view object that shows keys dynamically. It is lightweight but not subscriptable.
A subscriptable object supports indexing with square brackets. Lists, tuples, and strings are subscriptable. dict_keys is not. This is why you get the error.
Common Scenario
Imagine you have a dictionary and want the first key. You might write:
# Example that causes the error
my_dict = {"apple": 1, "banana": 2, "cherry": 3}
first_key = my_dict.keys()[0] # This raises TypeError
print(first_key)
TypeError: 'dict_keys' object is not subscriptable
The error message is clear. You tried to index a dict_keys object. Python does not allow this directly.
How to Fix It
The fix is simple. Convert the view object to a list first. Use the list() constructor. Here is the corrected code:
# Fix: convert dict_keys to a list
my_dict = {"apple": 1, "banana": 2, "cherry": 3}
keys_list = list(my_dict.keys())
first_key = keys_list[0] # Now works
print(first_key) # Output: apple
apple
Now you can access any index. This is the most straightforward solution. You can also use list(my_dict) directly because iterating over a dictionary gives its keys.
Alternative Solutions
There are other ways to get a specific key. Use next(iter(my_dict)) for the first key without creating a full list. This is memory-efficient for large dictionaries.
# Efficient way for first key only
my_dict = {"apple": 1, "banana": 2}
first_key = next(iter(my_dict))
print(first_key) # Output: apple
apple
If you need random access, always convert to a list. For simple iteration, use for key in my_dict: directly. This avoids the error entirely.
Why Did Python Change This?
Python 3 made dictionary views the default. Views are dynamic. They reflect changes in the dictionary. Lists are static. This change saves memory and improves performance. It also makes iteration faster.
The trade-off is that views are not subscriptable. You must explicitly convert them. This is a Python TypeError: Causes and Fixes pattern that beginners often encounter. Understanding it helps you write better code.
Preventing the Error
To avoid this error, remember these rules:
- Never use square brackets directly on
.keys(),.values(), or.items(). - Use
list()if you need indexing. - Use
next(iter())for the first element only. - Iterate directly over the dictionary when possible.
Check your Python version. If you are writing code for both Python 2 and 3, use list(my_dict.keys()) for compatibility. This ensures your code works everywhere.
Real-World Example
Suppose you have a dictionary of user scores. You want the top scorer's name. Here is a complete example:
# Real-world fix
scores = {"Alice": 95, "Bob": 87, "Charlie": 92}
# Get names as a list
names = list(scores.keys())
# Get scores as a list
score_values = list(scores.values())
# Find index of max score
max_index = score_values.index(max(score_values))
top_student = names[max_index]
print(f"Top student: {top_student}") # Output: Top student: Alice
Top student: Alice
This code converts both keys and values to lists. Then it finds the max score and the corresponding name. Without the conversion, you would get the error.
Common Mistakes with dict_values and dict_items
The same error applies to .values() and .items(). They return dict_values and dict_items objects. These are also not subscriptable. Use list() on them too.
# Error with dict_values
my_dict = {"a": 1, "b": 2}
# first_value = my_dict.values()[0] # This raises error
# Fix
first_value = list(my_dict.values())[0]
print(first_value) # Output: 1
1
Always treat these view objects the same way. Convert to a list for indexing. This is a key skill for any Python developer.
Using the sorted() Function
Sometimes you need keys in order. The sorted() function works directly on dictionaries. It returns a sorted list of keys. This avoids the error naturally.
# Sorted keys without error
my_dict = {"banana": 2, "apple": 1, "cherry": 3}
sorted_keys = sorted(my_dict)
print(sorted_keys) # Output: ['apple', 'banana', 'cherry']
['apple', 'banana', 'cherry']
This is clean and efficient. It shows that Python offers many ways to work with dictionaries without indexing view objects directly.
Conclusion
The TypeError: 'dict_keys' object is not subscriptable is a simple fix. Always convert dictionary views to lists before indexing. Use list(my_dict.keys()) or list(my_dict.values()). For the first key, use next(iter(my_dict)) for efficiency.
This error teaches an important lesson about Python 3 changes. View objects are powerful but not subscriptable. By understanding this, you avoid frustration and write cleaner code. Remember these tips and you will handle this error with confidence.
For more help with similar issues, check out our guide on Python TypeError: Causes and Fixes. It covers common errors and solutions for beginners.