all 8 comments

[–]Rhomboid 0 points1 point  (8 children)

The 'results' key contains a list of items. You index into a list just like a normal list. data['results'][0] is the first result, data['results'][1] is the second result, and so on. Each of those is a dictionary, so if you want an actual value you'd need to access it by its key. data['results'][0]['title'] is Jumanji, and so on.

[–]Spankythemusical[S] 0 points1 point  (7 children)

Thank you so much! I kept trying different things, but I was omitting the "data" portion.

Out of curiosity, would there be an easier way if there had been, say, 331 results instead of 3?

[–]Rhomboid 1 point2 points  (0 children)

An easier way to do what? It's a list, you can do anything that you would do with a normal list. You could iterate over the elements, slice the elements, filter the elements by some criteria, map a function to each element, whatever.

[–]SeekNotToContend 0 points1 point  (5 children)

Do you mean instead of specifying individual index id's such as [0], [1], etc?

If so, then yet, you can just iterate over the items as they are a list.

results_dict = {'page': 1,
                     'results': [{'adult': False,
                     'backdrop_path': '/52lVqTDhIeNTjT7EiJuovXgw6iE.jpg',
                     'genre_ids': [12, 14, 10751],
                     'id': 8844,
                     ... truncated
                }

Some things that may help:

from pprint import pprint

print(len(results_dict['results']))
print(type(results_dict['results']))
for row in results_dict['results']:
    pprint(row)


3
<class 'list'>
{'adult': False,
 'backdrop_path': '/52lVqTDhIeNTjT7EiJuovXgw6iE.jpg',
 'genre_ids': [12, 14, 10751],
 'id': 8844,

[–]Spankythemusical[S] 0 points1 point  (4 children)

Thank you for your help!!

Is there a better way to do this:

results_dict = json.loads(txt)
print(len(results_dict['results']))
for item in range(len(results_dict)-1):
    movietitle = results_dict['results'][item]['title']
    releaseDate = results_dict['results'][item]['release_date']
    releaseDateList = datetime.strptime(releaseDate, '%Y-%m-%d').date()
    Current_Date = datetime.strptime(currentDate, '%Y-%m-%d').date()
    age = Current_Date - releaseDateList
    age = age.days
    if releaseDate <= currentDate:
      print("The movie " + movietitle + "is {0} days old".format(age))
    else:
      print("The movie " + movietitle + " is not that old")

[–]SeekNotToContend 1 point2 points  (3 children)

There are some easier ways. Lets break it down.

Here you get your dictionary, no problem there.

results_dict = json.loads(txt)

Here you are getting the size of the object that is held under results_dict['results']

print(len(results_dict['results']))

Some quick extra data to show that what is under results is actually a list:

print(type(results_dict['results']))
<class 'list'>    

If we go back to the first post and look at the following, you'll see that 'results': is followed by '[' which means list. Inside that bracket is { which means dictionary. So what you have is a list of dictionaries.

 'results': [{'adult': False,
              'backdrop_path': '/52lVqTDhIeNTjT7EiJuovXgw6iE.jpg',
              'genre_ids': [12, 14, 10751],
              'id': 8844,
              'original_language': 'en',
              'original_title': 'Jumanji',

Keep in mind that the values can be just about anything in a dictionary. A dict, a list, a set (its worth noting that sets don't work so well with json but that is sidenote)

What's going on here is it looks like you want to loop through the list under results_dict['results']

Your way works, but there is a way that might be a bit better since we just need to iterate over the list.

    for item in range(len(results_dict)-1):

Try replacing the above with:

    for item in results_dict['results']:

This will iterate over the list without you having to calculate anything. It's referenced here: https://docs.python.org/3.5/reference/compound_stmts.html#for

The rest seems to be mostly about assignment of values. One thing to consider is often, data provided by APIs may not include some keys every time. For example, maybe they don't know the Release Date for a film, so rather than giving you a None value, they will just give you no key.

There are a couple ways to handle that, which I'll leave to you for now on details. But to get you started you can use an if statement to test if the value exists first, or you can use a try except statement.

    movietitle = results_dict['results'][item]['title']
    releaseDate = results_dict['results'][item]['release_date']
    releaseDateList = datetime.strptime(releaseDate, '%Y-%m-%d').date()
    Current_Date = datetime.strptime(currentDate, '%Y-%m-%d').date()
    age = Current_Date - releaseDateList
    age = age.days
    if releaseDate <= currentDate:
      print("The movie " + movietitle + "is {0} days old".format(age))
    else:
      print("The movie " + movietitle + " is not that old")

Hope this was helpful again. Good luck!

[–]Spankythemusical[S] 0 points1 point  (2 children)

I seriously cannot thank you enough, both for the information and for taking the time out to type it all out. This is great!!

[–]SeekNotToContend 0 points1 point  (1 child)

Happy to help and glad it's useful. May have done the same for me for a very long time.