Hurricane analysis Python, step 4

Python Dictionaries Challenge Project | Codecademy

Hello, I am stuck on step 4. With my function dict_year I manage to create a dictionary called hurricanes_by_year in order according to the year they happened. However, at the moment I have not figured out how to attach or update hurricanes that occurred in the same year to the same key with that year. E.g. 1932 should have two dictionaries, but I only manage to attach one dictionary as the value.

Is there a way to append(), as with lists, a new value to a key that already has values?

I believe the instructions state you should be using a container type e.g. a list as the value associated with each key.

I think it might be suggested at one point but defaultdict from the collections module can simplify your code for this step (it’s typically considered a little more readable than using .setdefault).

Thank you very much for the pointer, I didn’t notice that. I think I finally solved it, for some reason it was the step I was finding the hardest.

1 Like

Glad you got it sorted. I would suggest using a different parameter name than dict though as it masks the built-in dict() class within that function (something you should avoid if possible even if it’s for the sake of others viewing your code).

1 Like