Hey everyone,
So I’m working through the Hurricane Analysis set of problems in the Data Science track, and I’ve been having issues with #3 which is where we would need to create a new dictionary with existing values from the older dictionary but the keys are different.
I’m working off platform and I have the Codecademy solution handy.
For context, the starting dictionary has 34 entries. I expected that the new dictionary will have the same 34 entries, as we’re not popping or manipulating any of the data, just changing the key from Name to Year.
Here is my solution:
def testing(hurricanes):
year_hurricane = {}
for key, value in hurricane_dictionary.items():
for year in range(len(years)):
key = years[year]
year_hurricane[key] = [value]
return year_hurricane
print(testing(hurricane_dictionary))
when you check the length, it’s 26, meaning for some reason it removed 8 items. i struggled for a day half with this problem until i gave up and just copied the codecademy solution:
codecademy 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(len(hurricanes_by_year))
As you can see, the length of this new dictionary on the codecademy side is also 26.
Can anyone tell me what’s going on? Why is the solution code coming up 8 items short as well? I don’t fully understand what’s going on in the backend or the logic behind why both my code and the solution comes up short.
Would really appreciate all your help!