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

So, I’m trying to capture and store wave height data and cache it in a file based on the fetch timestamp in local time.
I have hit an error with the last section.

Here is the error -

    d = sorted(data['hours'], key=lambda i: i['time'])
TypeError: list indices must be integers or slices, not str

I have tried to fix it but I have no idea how to.

Here is the full code -
https://gist.githubusercontent.com/Xioto/cad6d50699d7a6599757172ab0847b93/raw/bb28c822b4df5ae1a925bc822a9204a91e57582f/main.py

Edit - I apologise for all the random comments, I am a bit messy

Hello, @xioto0110350088, and welcome to the forums.

I took a brief look at your code, and this comment struck me:
# data is an array of dictionaries with 'hours' and some request tracking
So data is a list of dictionaries. The elements of a list are accessed by their index as the error message states. If data were a dictionary with a key named hours you could access it with data['hours'], but it is a list.

1 Like