In this article, we will discuss about the reasons of getting error ‘TypeError: ‘dict_values’ 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 returns 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.
TypeError: ‘dict_values’ object does not support indexing
The dict.values() function returns a view object and 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_values’ object does not support indexing
For example,
Frequently Asked:
# Dictionary of string and int word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } # Fetch a view object pointing to all views of dictionary values = word_freq.values() print('dict_values view object:') print(values) print('Try to perform indexing:') # Try to perform indexing on the value's view object (dict_values) will cause error first_value = values[0]
Output
dict_values view object: dict_values([56, 23, 43, 78, 11]) Try to perform indexing: Traceback (most recent call last): File "temp.py", line 18, in <module> first_value = values[0] TypeError: 'dict_values' object does not support indexing
As we were trying to select value at index 0 from the dict_values object, which is a view object. This view doesn’t supports the indexing, therefore, it raised a Type error i.e.
TypeError: ‘dict_values’ object does not support indexing
How to resolve – TypeError: ‘dict_values’ object does not support indexing
Now to avoid this error, we can convert the view object dict_values into a list and then perform indexing on that. For example, we can cast the dict_values 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 views of dictionary values = list(word_freq.values()) print('dict_values view object:') print(values) # Try to perform indexing on the value's view object (dict_values) will cause error first_value = values[0] print('First Value: ', first_value)
Output:
dict_values view object: [56, 23, 43, 78, 11] First Value: 56
Here, we converted all the values from dictionary to list and then selected 1st element from the list, which is basically the first value from the first key-value pair of the dictionary.
Further Learning
- How to get first key in Dictionary – Python?
- How to get first value in a dictionary in python?
- Hoe to get first key-value pair from a Python Dictionary?
Conclusion:
In this article we learned that view object (dict_values) returned by dict.values() doesn’t supports the indexing. If someone tries to select element from dict_values 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.