This error occurs when you try to access a Response object using the subscript operator []
, without first parsing the object to a subscriptable Python object. You can solve this error by converting the Response object into a subscriptable Python object. For example,
import requests import json url = 'https://reqres.in/api/users' payload = {'name':'electron', 'charge':-1, 'mass':0.511} response = requests.post(url, data=payload) json_resp = response.json() print(json_resp['charge'])
This tutorial will go through the error in detail and how to solve it with code examples.
TypeError: ‘Response’ object is not subscriptable
TypeErrror occurs when you attempt to perform an illegal operation for a particular data type. The part “‘method’ object is not subscriptable” tells us that method is not a subscriptable object. Subscriptable objects have a __getitem__
method, and the subscript operator []
, to retrieve individual items.
Examples of subscriptable objects are lists, dictionaries and tuples. We can check if an object has __getitem__
is an attribute of an object using the built-in dir function. Let’s use the dir()
function to check if __getitem__
is an attribute of a Response object.
import requests import json url = 'https://reqres.in/api/users' payload = {'name':'electron', 'charge':-1, 'mass':0.511} response = requests.post(url, data=payload) print(type(response)) print('__getitem__' in dir(response))
<class 'requests.models.Response'> False
We can see that __getitem__
is not an attribute of the Response object
Example
Let’s look at an example of creating a Response object and trying to access its values.
import requests import json url = 'https://reqres.in/api/users' payload = {'name':'electron', 'charge':-1, 'mass':0.511} response = requests.post(url, data=payload) print(response['charge'])
In the above code, we define a POST request to Reqres and store the Response object under the variable name response
. Then, we try to get the ‘charge
‘ value from the object using the key ‘charge
‘ with square brackets. Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [41], in <cell line: 10>() 6 payload = {'name':'electron', 'charge':-1, 'mass':0.511} 8 response = requests.post(url, data=payload) ---> 10 print(response['charge']) TypeError: 'Response' object is not subscriptable
The error occurs because the Response object does not automatically provide the response data in the form of keys and values. We need to parse the Response object into a subscriptable Python object.
Solution
We can solve this error by parsing the Response object into a Python dictionary using the json()
method. Let’s look at the revised code:
import requests import json url = 'https://reqres.in/api/users' payload = {'name':'electron', 'charge':-1, 'mass':0.511} response = requests.post(url, data=payload) print(type(response)) json_resp = response.json() print(type(json_resp)) print(json_resp['charge'])
Let’s run the code to see the result:
<class 'requests.models.Response'> <class 'dict'> -1
We successfully made the post request, parsed the response object into a Python dictionary and then accessed the charge value using the subscript operator []
.
Summary
Congratulations on reading to the end of this tutorial!
For further reading on related TypeErrors:
- How to Solve Python TypeError: ‘builtin_function_or_method’ object is not subscriptable
- How to Solve Python TypeError: Object of type Response is not JSON serializable
- How to Solve Python TypeError: DictProxy object is not JSON serializable
To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.