Python dictionary list with multiple keys

Viewed 71

I am fairly new to Python, so I apologize for any mistakes found within my post. I have recently been playing around with APIs with Python and came across an API that responds to my request with a list of dictionaries for films for the respective studio providing the API (Not sure if it is a JSON list of dictionaries) From the list of dictionaries I am trying to create a for-loop to get rotten tomato scores greater than 90, but I would also like to get the corresponding title of the movie within the for-loop. I have tried adding if key == 'rt_score' and 'title', but I don't know if this is the correct approach and whether it would lead me to the correct solution. Here is my code:

import requests
import json

response = requests.get("https://ghibliapi.herokuapp.com/films")

print(response.status_code)
json_response = response.json()

for dicti in json_response:
    for key in dicti:
        if key == 'rt_score':
            rt_score = int(dicti[key])
            if rt_score > 90:
                print(str(key) + ': ' + str(rt_score))

This is what the JSON list of dictionaries looks like:

[
   {
      "description": "The orphan Sheeta inherited a mysterious crystal that links her to the mythical sky-kingdom of Laputa. With the help of resourceful Pazu and a rollicking band of sky pirates, she makes her way to the ruins of the once-great civilization. Sheeta and Pazu must outwit the evil Muska, who plans to use Laputa's science to make himself ruler of the world.",
      "director": "Hayao Miyazaki",
      "id": "2baf70d1-42bb-4437-b551-e5fed5a87abe",
      "locations": [
         "https://ghibliapi.herokuapp.com/locations/"
      ],
      "people": [
         "https://ghibliapi.herokuapp.com/people/"
      ],
      "producer": "Isao Takahata",
      "release_date": "1986",
      "rt_score": "95",
      "species": [
         "https://ghibliapi.herokuapp.com/species/af3910a6-429f-4c74-9ad5-dfe1c4aa04f2"
      ],
      "title": "Castle in the Sky",
      "url": "https://ghibliapi.herokuapp.com/films/2baf70d1-42bb-4437-b551-e5fed5a87abe",
      "vehicles": [
         "https://ghibliapi.herokuapp.com/vehicles/"
      ]
   },
3 Answers

To iterate over the keys you need to call .keys() on the dictionary, but you do not need to do that. Just use the key with the dictionary like shown below.

for dict_ in json_response:
    if float(dict_['rt_score']) > 90:
        title = dict_['title']
        print(title)

You can access the title within your for-loop by dicti['title'].

Full example using your code:

import requests
import json

response = requests.get("https://ghibliapi.herokuapp.com/films")

print(response.status_code)
json_response = response.json()

for dicti in json_response:
    for key in dicti:
        if key == 'rt_score':
            rt_score = int(dicti[key])
            if rt_score > 90:
                print(dicti['title'] + ' - ' str(key) + ': ' + str(rt_score))

A simple way:

for film in json_response:
    if int(film['rt_score']) > 90:
        print(f"{film['title']}: {film['rt_score']}")

This uses a f-string for printing, so needs at least Python 3.6.

Related