1

I would like to sort a list of books that is returned by the Google Books API by the date published of the books. The data looks something like this:

{
 "kind": "books#volumes",
 "totalItems": 506,
 "items": [
   {
  ...
     "volumeInfo": {
       "title": "RHYTHM OF WAR PART ONE",
       "authors": [
         "BRANDON SANDERSON"
       ],
       "publishedDate": "2023-03-16",
       "industryIdentifiers": [
         {
           "type": "ISBN_10",
           "identifier": "1473233372"
         },
   ...
   },

What I have tried is first first isolating the books in a new list like so:

myList = myList["items"]

And then sorting the new list by datetime.

myListSorted = sorted(myList, key=lambda d: datetime.datetime.strptime(d["volumeInfo"]["publishedDate"], '%Y-%m-%d'))

I am getting the following error message:

myListSorted = sorted(myList, key=lambda d: datetime.datetime.strptime(d["publishedDate"]["volumeInfo"], '%Y-%m-%d'))

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

I have also tried using the itemgetter method, but have not been successful so far.

The results of the API calls can be sorted by date published like so:

https://www.googleapis.com/books/v1/volumes?q=inauthor:brandon%20sanderson&orderBy=newest

But I am adding the results of several calls together in one list and would like to be able to sort all of the books by release date.

1
  • The error message indicates that you're indexing into something that's a list rather than a dict - that is, d or (if the error is actually from the earlier line) myList is not what you think it is or is not from data that's exactly as shown. My guess is that there's something off about how you've assigned myList, this code snippet isn't exactly wrong but it's suspicious: myList = myList["items"]. I would usually give the items-only list a new name for clarity, eg volumes = myList["items"]. Commented Dec 22, 2021 at 23:31

2 Answers 2

2

I figured out your problem!

In your second myListSorted statement, you got the dictionary arguments the wrong way around!

Try this:

myListSorted = sorted(myList, key=lambda d: datetime.datetime.strptime(d["volumeInfo"]["publishedDate"], '%Y-%m-%d'))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I had tried to switch around the arguments when it didn't work and accidentally copied the error message for an earlier execution of the code. I have since figured out that things were going wrong at a different point in my code.
@MD, no problem! Glad you figured it out!
0

The problem must be elsewhere in your code, as this does minimally what you said you did, but has no issues:

from json import load
from urllib.request import urlopen
from datetime import datetime

with urlopen('https://www.googleapis.com/books/v1/volumes?q=inauthor:brandon%20sanderson&orderBy=newest') as r:
    data = load(r)
    items = data['items']
    sorted_items = sorted(items, key=lambda d: datetime.strptime(d["volumeInfo"]["publishedDate"], '%Y-%m-%d'))

print(sorted_items)

Please provide a minimal, reproducible example, if you do still have a problem.

1 Comment

Thank you for pointing that out. I have been adding the results of the API calls together incorrectly. Now I am getting ValueError: time data '2021' does not match format '%Y-%m-%d' so I am going to look into dealing with values that do not have the format I have specified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.