I’ve gotten totally hung up on question 4 of the Python Dictionary Hurricanes Challenge Project. The question asks you to write a function that converts the current dictionary of hurricanes to a new dictionary, where the keys are years and the values are lists containing a dictionary for each hurricane that occurred in that year.
I feel like my answer and the answer provided in Codecademy’s solution are fundamentally trying to do the same thing. But my solution throws the error "AttributeError: ‘dict’ object has no attribute ‘append.’ I’ve looked around the internet about this error, and I keep seeing different threads on whether appending a dictionary is even possible.
Does the dict() object have something to do with this? I feel like it was brushed over in the course material, but was used a few times in the provided solution for this challenge.
This is Codecademy’s solution.
def create_year_dictionary(hurricanes): hurricanes_by_year= dict() for cane in hurricanes: current_year = hurricanes[cane]['Year'] current_cane = hurricanes[cane] if current_year not in hurricanes_by_year: hurricanes_by_year[current_year] = [current_cane] else: hurricanes_by_year[current_year].append(current_cane) return hurricanes_by_year hurricanes_by_year = create_year_dictionary(hurricanes) print(hurricanes_by_year)
This is my solution that gets the error "AttributeError: ‘dict’ object has no attribute ‘append’
def create_year_dictionary(): hurricanes_by_year = dict() for key, value in hurricanes.items(): current_year = value["Year"] current_hurricane = key if current_year not in hurricanes_by_year: hurricanes_by_year[current_year] = value else: hurricanes_by_year[current_year].append(current_hurricane) return hurricanes_by_year print(create_year_dictionary())
Any help would be appreciated, thank you!