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.
dor (if the error is actually from the earlier line)myListis 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 assignedmyList, 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, egvolumes = myList["items"].