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

Looping through list to print each value from each key in loop

I have put together a script to output all fields from a json file.

I have running into issues getting it to print each finding from highsev and specifically the id field from each finding.

Here is the code:

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

json_object=json.load(f)
vulnsBySeverity["HIGHSEVERITY"] = []
for result in json_object['results']:
    vulnsBySeverity["HIGHSEVERITY"] = [vuln for vuln in result['vulnerabilities'] if vuln["severity"] == "high"]

highsev=vulnsBySeverity["HIGHSEVERITY"]
print("HIGH VULNS were")
for finding in highsev:
    print(highsev[finding]['id'])

Its complaining :

TypeError: list indices must be integers or slices, not dict

I think I understand what its saying that finding call in the print() has to be a int or slice im just wondering how i can do that, as finding contains this:

{'id': 'CVE-2020-15999'}

Im simply just trying to loop through highsev and output the value of id in a loop.

What might i be doing wrong here? can someone please help

>Solution :

You’re already looping through highsev with your for finding in highsev:

If your highsev list contains dictionaries like this {'id': 'CVE-2020-15999'}, you can do this:

for finding in highsev:
    print(finding['id'])
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