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

Add blank value for key if does not exist when appending dictionaries to list

I have the following code which uses an API to download data from a website.

url = "https://gtr.ukri.org/project"
params = {"page": 11,
          "fetchSize": 100}
projects = requests.get(url,
                        params=params,
                        headers={"accept": "application/json"}).json()['projectsBean']['project']

for project in projects:
    list_projects.append({'ref': project['grantReference'], 'funder': project['fund']['funder']['name'],
                          'title': project['title'], 'category': project['grantCategory'],
                          'abstract': project['abstractText'], 'funding': project['fund']['valuePounds'],
                          'start': project['fund']['start'], 'end': project['fund']['end']})

This is working fine, however sometimes there is no value for a key when iterating through all the pages in the website (the key will always exist).

When this happens, it errors as a value is not there.

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

How do I append a dictionary to a list and just leave any values for any key blank if it does not exist, rather than erroring?

Many thanks

>Solution :

To have a default value when no value exists in your dictionary you can do it like this

project.get('grantReference', 'no-ref')

the string 'no-ref' is a default value if the project dict doesn’t contain any key named grantReference.

the above is only an example to do get a value of dict with key without error.

but however, on your question

This is working fine, however sometimes there is no value for a key when iterating through all the pages in the website (the key will always exist).

you doesn’t mention the exact code line where the error come from so I can not help to create a complete example code.

For nested dictionary you can use the method like this

project.get('fund', {}).get('start', 0)

the first key 'fund' will return value that you expecting or will default to be an empty dictionary. so the second key 'start' will get value with key 'start' from the dictionary you expecting or from the default empty dictionary we provide. (and we provide default value for key 'start' which is the 0)

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