How to Solve Python TypeError: ‘Response’ object is not subscriptable

by | Programming, Python, Tips

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!

How to Solve Python TypeError: ‘builtin_function_or_method’ object is not subscriptable

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!