Question/Issue with int() function

New to coding so I’m struggling a bit. I have a list of dictionaries that I need to re-format using the key “year” which is a string. So I know I need to use the int() function but I must not be doing it right. Here is the code:

works_by_year = []
for author_dict in works:
    year = int(author_dict["year"])
    if year not in works_by_year:
        works_by_year["year"] = []

works_by_year[“year”] =
TypeError: list indices must be integers or slices, not str

Can someone please explain to me what I am doing wrong and how to fix it?

Error says that it is tried to use string as index of the list.

The string in question here is "year", and the list - works_by_year.

You can access a list item
list =
list[n] where n is integer

You can access item in dictionary
dict = {}
dict[“key”] where key can be integer or string whatever you use

Instead of list you should use dictionary
works_by_year = {}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.