In this article, we will discuss about the reasons of getting error ‘TypeError: ‘dict_keys’ object does not support indexing’ and how to resolve this error.
A little background,
In Python, the dictionary class provides functions keys(), values() and items() to fetch keys, values or all items (key-value pairs). But all these functions return view objects, which provides a dynamic view on the dictionary entries.
When a dictionary changes then the view reflects these changes too. We can iterate over these view objects, but we can not use indexing on these view objects. If we try to do that, then it will cause TypeError. As the dict.keys() function returns a view object, therefore we can not use the indexing on the view objects. If we try to select elements from it using indexing, then we will get this error,
TypeError: ‘dict_keys’ object does not support indexing
For example,
# Dictionary of string and int word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } # Fetch a view object pointing to all keys of dictionary keys = word_freq.keys() print('dict_keys view object:') print(keys) print('Try to perform indexing:') # Try to perform indexing on the key's view object will cause error first_key = keys[0] print('First Key: ', first_key)
Output:
Frequently Asked:
Try to perform indexing: Traceback (most recent call last): File "temp.py", line 18, in <module> first_key = keys[0] TypeError: 'dict_keys' object does not support indexing
As we were trying to select value at index 0 from the dict_keys object, which is a view object. This view doesn’t supports the indexing, therefore, it raised a Type error i.e.
TypeError: ‘dict_keys’ object does not support indexing
How to resolve – TypeError: ‘dict_keys’ object does not support indexing
Now to avoid this error, we can convert the view object dict_keys into a list and then perform indexing on that. For example, we can cast the dict_keys object to a list object and then select element at any index from it. Let’s see the code for it,
# Dictionary of string and int word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } # Fetch a view object pointing to all keys of dictionary keys = list(word_freq.keys()) print('List of Keys:') print(keys) # Select 2nd element from keys list second_key = keys[1] print('Second Key: ', second_key)
Output:
List of Keys: ['Hello', 'at', 'test', 'This', 'Why'] Second Key: at
Here, we converted all the keys from dictionary to list and then selected 2nd element from the list, which is basically second key from the dictionary.
Further Learning:
- How to get first key-value pair from a Python Dictionary ?
- How to get first key in python Dictionary ?
- How to get first value in a python dictionary ?
Conclusion:
In this article we learned that view object (dict_keys) returned by dict.keys() doesn’t supports the indexing. If someone tries to select element from dict_keys using indexing, then it will raise TypeError. To avoid that error, we can cast it to list first and then select items from it using indexing.