Hurricane Analysis - Project

Hello everyone!

I am currently trying to complete a project and I have ran into a problem. I am unsure of how to work through the Hurricane Analysis project where it asks you to create a function that takes the previous dictionary and reorganize it so that it has the years as the keys and data on the hurricanes from that year as the values. I am attaching my code here but the output is not what I am expecting. It in fact just outputs some names and a bunch of random letters. Please help, anyone!

Incomplete Hurricane Analysis Project - Stuck on Part 3

I think you mixed up list stuff and dictionary stuff in your function.

The parameter of the function, which you have as dictionary, is a dictionary where the keys are hurricanes’ names (strings), and the values are dictionaries which contain information about the hurricane.

When you do
for hurricane in dictionary:  
you’re iterating through the keys … 'Cuba I', 'San Felipe', 'Bahamas', and so on.
These are strings, so hurricane[2] is giving you the letter in the string at index 2.

You seem to want to iterate through the values, so  
for hurricane in dictionary:  
should be  
for hurricane in dictionary.values():  
so that hurricane is a dictionary.

And since hurricane is a dictionary, not a list,
you can’t do  
current_year = hurricane[2]  
since 2 is not one of the keys of that dictionary.
That should be  
current_year = hurricane["Year"]  
instead.

The function should return a dictionary, where the keys are years (integers) and the values are lists (of dictionaries).
So this code in the loop is a problem:

    elif current_year in hurricanes_by_year:
      hurricanes_by_year.update({current_year: current_cane})

.update({current_year: current_cane}) replaces the value for the key indicated by current_year, so that the new value for that key becomes the dictionary from current_cane
But you shouldn’t be replacing the list there with a hurricane dictionary,
you should be appending that dictionary to the list instead.

    elif current_year in hurricanes_by_year:
      hurricanes_by_year[current_year].append(current_cane)
3 Likes

Wow!
Thank you so much. I was definitely mixing my list functions with dictionary functions. It works with these updates. You are so smart!