Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filtering dictionary of Response objects by value

I have my response dictionary.

Example:

{10874: <Response [400]>, 11233: <Response [400]>, 13360: <Response [400]>, 15008: <Response [400]>, 11638: <Response [200]>, 14150: <Response [400]>, 15323: <Response [400]>, 14814: <Response [400]>, 12007: <Response [400]>, 11337: <Response [400]>, 13342: <Response [200]>}

I’m trying to create new dictionary with only response 200.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My code:

new_dict = {}
for k,v in results.items():
    if v == "<Response [200]>":
        new_dict[k] = v

But I’m not able to filter. Type of dictionary value is class ‘requests.models.Response’.

>Solution :

Filter Response objects by their status_code attribute:

new_dict = {k:resp for k,resp in results.items() if resp.status_code == 200}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading