10-17-2022, 03:24 AM
Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug)
5/5 – (1 vote)
Do you encounter the following error message?
You’re not alone! This short tutorial will show you why this error occurs, how to fix it, and how to never make the same mistake again.
So, let’s get started!
Solution
Python raises the “” if you use indexing or slicing on the object obtained with . To solve the error, convert the object to a list such as in .
Example
![[Image: image-135.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-135.png)
The following minimal example that leads to the error:
Output:
Note that the same error message occurs if you use slicing instead of indexing:
Fixes
The reason this error occurs is that the method returns a object that is not subscriptable.
You can use the function to check it for yourself:
Note: You cannot expect dictionary keys to be ordered, so using indexing on a non-ordered type wouldn’t make too much sense, would it?![[Image: 26a1.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/26a1.png)
You can fix the non-subscriptable TypeError by converting the non-indexable object to an indexable container type such as a listin Python using the or function.
Here’s an example fix:
Here’s an other example fix:
Both lists and tuples are subscriptable so you can use indexing and slicing after converting the object to a list or a tuple.
Full Guide: Python Fixing This Subsctiptable Error (General)
Summary
Python raises the if you try to index or slice a object.
The type is not indexable, i.e., it doesn’t define the method. You can fix it by converting the dictionary keys to a list using the built-in function.
Alternatively, you can also fix this by removing the indexing or slicing call, or defining the method. Although the previous approach is often better.
What’s Next?
I hope you’d be able to fix the bug in your code! Before you go, check out our free Python cheat sheets that’ll teach you the basics in Python in minimal time:
If you struggle with indexing in Python, have a look at the following articles on the Finxter blog—especially the third!
Related Articles:
5/5 – (1 vote)
Do you encounter the following error message?
Code:
TypeError: 'dict_keys' object is not subscriptableYou’re not alone! This short tutorial will show you why this error occurs, how to fix it, and how to never make the same mistake again.
So, let’s get started!
Solution
Python raises the “
Code:
TypeError: 'dict_keys' object is not subscriptableCode:
dict_keysCode:
dict.keys()Code:
dict_keysCode:
list(my_dict.keys())[0]Code:
print(list(my_dict.keys())[0])Example
![[Image: image-135.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-135.png)
The following minimal example that leads to the error:
Code:
d = {1:'a', 2:'b', 3:'c'}
print(d.keys()[0])Output:
Code:
Traceback (most recent call last): File "C:\Users\...\code.py", line 2, in <module> print(d.keys()[0])
TypeError: 'dict_keys' object is not subscriptableNote that the same error message occurs if you use slicing instead of indexing:
Code:
d = {1:'a', 2:'b', 3:'c'}
print(d.keys()[:-1]) # <== same errorFixes
The reason this error occurs is that the
Code:
dictionary.keys()Code:
dict_keysYou can use the
Code:
type()Code:
print(type(d.keys()))
# <class 'dict_keys'>Note: You cannot expect dictionary keys to be ordered, so using indexing on a non-ordered type wouldn’t make too much sense, would it?
![[Image: 26a1.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/26a1.png)
You can fix the non-subscriptable TypeError by converting the non-indexable
Code:
dict_keysCode:
list()Code:
tuple()Here’s an example fix:
Code:
d = {1:'a', 2:'b', 3:'c'}
print(list(d.keys())[0])
# 1Here’s an other example fix:
Code:
d = {1:'a', 2:'b', 3:'c'}
print(tuple(d.keys())[:-1])
# (1, 2)Both lists and tuples are subscriptable so you can use indexing and slicing after converting the
Code:
dict_keys
Full Guide: Python Fixing This Subsctiptable Error (General)Summary
Python raises the
Code:
TypeError: 'dict_keys' object is not subscriptableCode:
x[i]Code:
x[i:j]Code:
dict_keysThe
Code:
dict_keysCode:
__getitem__()Code:
list()Alternatively, you can also fix this by removing the indexing or slicing call, or defining the
Code:
__getitem__What’s Next?
I hope you’d be able to fix the bug in your code! Before you go, check out our free Python cheat sheets that’ll teach you the basics in Python in minimal time:
If you struggle with indexing in Python, have a look at the following articles on the Finxter blog—especially the third!
Related Articles:

![[Image: 2b50.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png)