TypeError: 'dict_keys' object is not subscriptable (Python)

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: 'dict_keys' object is not subscriptable (Python) #

The Python "TypeError: 'dict_keys' object is not subscriptable" occurs when we try to access a dict_keys object at a specific index. To solve the error, convert the dict_keys object to a list, before accessing an index, e.g. list(my_dict.keys())[0].

typeerror dict keys object is not subscriptable

Here is an example of how the error occurs.

main.py
my_dict = {'name': 'Alice', 'age': 30} keys = my_dict.keys() print(type(keys)) # 👉️ <class 'dict_keys'> # ⛔️ TypeError: 'dict_keys' object is not subscriptable print(keys[0])

Notice that the dict.keys() method returns a dict_keys object, not a list.

To solve the error, pass the dict_keys object to the list constructor to convert it to a list before accessing a list item at a specific index.

main.py
my_dict = {'name': 'Alice', 'age': 30} # ✅ convert to list keys = list(my_dict.keys()) print(keys[0]) # 👉️ "name" print(keys[1]) # 👉️ "age" print(keys[0:2]) # 👉️ ['name', 'age']

The dict.keys method returns a new view of the dictionary's keys.

Since dict_keys objects are not subscriptable, we can't access them at a specific index.

Note that if you try to access a list index that is out of bounds, you would get an error. You can use a try/except statement if you need to handle that.

main.py
my_dict = {'name': 'Alice', 'age': 30} keys = list(my_dict.keys()) try: print(keys[100]) except IndexError: print('index out of bounds') # 👉️ this runs

The example catches the IndexError that is thrown if the index is out of bounds.

The list constructor takes an iterable, such as a dict_keys object, and converts it to a list.

The list constructor builds a list whose items are the same and in the same order as the iterable's items.

The list constructor takes an iterable that may be a sequence, a container that supports iteration or an iterator object.

You should only use square brackets to access subscriptable objects.

The subscriptable objects in Python are:

  • list
  • tuple
  • dictionary
  • string

All other objects have to be converted to a subscriptable object by using the list(), tuple(), dict() or str() classes to be able to use bracket notation.

Subscriptable objects implement the __getitem__ method whereas non-subscriptable objects do not.

main.py
a_list = [2, 4, 6, 8] # 👇️ <built-in method __getitem__ of list object at 0x7f71f3252640> print(a_list.__getitem__)

Conclusion #

The Python "TypeError: 'dict_keys' object is not subscriptable" occurs when we try to access a dict_keys object at a specific index. To solve the error, convert the dict_keys object to a list, before accessing an index, e.g. list(my_dict.keys())[0].

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.