Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug)

#1
Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug)

5/5 – (1 vote)

Do you encounter the following error message?

TypeError: 'dict_keys' object is not subscriptable

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 “TypeError: 'dict_keys' object is not subscriptable” if you use indexing or slicing on the dict_keys object obtained with dict.keys(). To solve the error, convert the dict_keys object to a list such as in list(my_dict.keys())[0].

print(list(my_dict.keys())[0])

Example



The following minimal example that leads to the error:

d = {1:'a', 2:'b', 3:'c'}
print(d.keys()[0])

Output:

Traceback (most recent call last): File "C:\Users\...\code.py", line 2, in <module> print(d.keys()[0])
TypeError: 'dict_keys' object is not subscriptable

Note that the same error message occurs if you use slicing instead of indexing:

d = {1:'a', 2:'b', 3:'c'}
print(d.keys()[:-1]) # <== same error

Fixes


The reason this error occurs is that the dictionary.keys() method returns a dict_keys object that is not subscriptable.

You can use the type() function to check it for yourself:

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? ⚡

You can fix the non-subscriptable TypeError by converting the non-indexable dict_keys object to an indexable container type such as a list in Python using the list() or tuple() function.

Here’s an example fix:

d = {1:'a', 2:'b', 3:'c'}
print(list(d.keys())[0])
# 1

Here’s an other example fix:

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 dict_keys object to a list or a tuple.

? Full Guide: Python Fixing This Subsctiptable Error (General)

Summary


Python raises the TypeError: 'dict_keys' object is not subscriptable if you try to index x[i] or slice x[i:j] a dict_keys object.

The dict_keys type is not indexable, i.e., it doesn’t define the __getitem__() method. You can fix it by converting the dictionary keys to a list using the list() built-in function.

Alternatively, you can also fix this by removing the indexing or slicing call, or defining the __getitem__ 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:



https://www.sickgaming.net/blog/2022/10/...tupid-bug/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] (Fixed) Python TypeError ‘bool’ object is not subscriptable xSicKxBot 0 1,436 10-02-2022, 12:23 PM
Last Post: xSicKxBot
  [Tut] Python TypeError ‘set’ object is not subscriptable xSicKxBot 0 1,220 09-11-2022, 08:12 AM
Last Post: xSicKxBot
  [Tut] Python TypeError: NoneType is Not Subscriptable (Fix This Stupid Bug) xSicKxBot 0 1,126 09-06-2022, 02:00 AM
Last Post: xSicKxBot
  [Tut] [Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable xSicKxBot 0 1,306 09-05-2022, 09:33 AM
Last Post: xSicKxBot
  [Tut] How to Fix TypeError: unhashable type: ‘list’ xSicKxBot 0 1,350 07-14-2022, 03:51 AM
Last Post: xSicKxBot
  [Tut] Python Math Domain Error (How to Fix This Stupid Bug) xSicKxBot 0 1,258 11-21-2020, 05:01 AM
Last Post: xSicKxBot
  [Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) xSicKxBot 0 1,336 11-12-2020, 09:04 PM
Last Post: xSicKxBot
  [Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? xSicKxBot 0 1,218 10-16-2020, 07:30 PM
Last Post: xSicKxBot
  [Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) xSicKxBot 0 1,320 10-10-2020, 05:24 PM
Last Post: xSicKxBot
  [Tut] How to Solve Python “TypeError: ‘int’ object is not iterable”? xSicKxBot 0 1,319 09-18-2020, 10:49 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016