How to fix this “typeerror: ‘dict_keys’ object is not subscriptable” error message?
If you’re dealing with this error right now and you’re struggling with how to resolve it, keep reading.
In this article, we’ll discuss in detail what this “dict_keys object is not subscriptable” means, why it occurs, and the different solutions to fix the error.
What is “typeerror: ‘dict_keys’ object is not subscriptable”?
The “typeerror: ‘dict_keys’ object is not subscriptable” is an error message in Python that occurs when you are trying to access (index or slice) a ‘dict_keys’ object.
In addition to that, this error happens when you are trying to use the “dict_keys” object as an index, in arithmetic operations, or when slicing or subscripting, which is not allowed.
For example:
sample_dict = {'website': 'Itsourcecode'}
keys = sample_dict.keys()
print(keys[0])
As a result, it will throw an error:
TypeError: 'dict_keys' object is not subscriptable
How to fix “typeerror: ‘dict_keys’ object is not subscriptable”?
To fix this “dict_keys object is not subscriptable” typeerror, convert the dict_keys object to a subscriptable object, such as a list list(my_dict.keys())[0] or a tuple.
Here are the following solutions that you may use to fix this error:
1: Convert dict_keys object to a list
You can resolve the error by converting the dict_keys object to a list using the list() function.
Right after that, you can access the desired key using indexing or slicing.
For example:
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd':4, 'e':5}
keys = list(my_dict.keys())
print(keys[0])
print(keys[1])
print(keys[2])
print(keys[3])
print(keys[4])
Output:
a
b
c
d
e
Convert the dict_keys object to a tuple:
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd':4, 'e':5}
keys = list(my_dict.keys())
keys = tuple(keys)
print(keys[0])
print(keys[1])
print(keys[2])
print(keys[3])
print(keys[4])
You can resolve the error by converting the dict_keys object to a list using the tuple() function.
Output:
a
b
c
d
e
2: Use for loop
You can also use a for loop to iterate over the dict_keys object so that you can access each key individually.
It will print each key in the my_dict dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd':4, 'e':5}
keys = list(my_dict.keys())
for key in keys:
print(key)
Output:
a
b
c
d
e
3: Use list comprehension
Alternatively, you can also use a comprehension to create a list of keys from a dict_keys object.
As you can see in this example code, we use list comprehension to create a list of keys from the my_dict dictionary and then access it using for key in keys:
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd':4, 'e':5}
keys = [key for key in my_dict]
for key in keys:
print(key)
Output:
a
b
c
d
e
4: Use sorted() function
You can use the built-in function sorted() to sort the keys before looping. In this case, the code will sort the keys using the “sorted function,” which returns a sorted list of keys that can be used in the for loop.
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd':4, 'e':5}
keys = sorted(my_dict.keys())
for key in keys:
print(key)
Output:
a
b
c
d
e
5: Use dict() constructor
You can use the dict() constructor in order to create a new dictionary with the same key-value pairs.
The example code creates a new dictionary, new_dict, using the dict() constructor, which copies the key-value pairs from my_dict.
Then, you can loop through the keys in new_dict.
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd':4, 'e':5}
new_dict = dict(my_dict)
for key in new_dict:
print(key)
Output:
a
b
c
d
e
Conclusion
In conclusion, the “Typeerror: ‘dict_keys’ object is not subscriptable” occurs when occurs when you are trying to access (index or slice) a ‘dict_keys’ object.
Fortunately, this article provided several solutions above so that you can fix the “dict_keys object is not subscriptable” error message.
We are hoping that this article provided you with sufficient solutions to get rid of the error.
You could also check out other “typeerror” articles that may help you in the future if you encounter them.
- Typeerror: object of type ndarray is not json serializable
- Typeerror: res.status is not a function
- Typeerror: method object is not subscriptable
Thank you very much for reading to the end of this article.