Hurricane Analysis Project - Areas Affected Dictionary creation

I need to iterate through an existing dictionary of dictionaries which this structure for each of the entries in it:

{Name: {“Name”: names[index], “Month”: months[index], “Year”: years[index], “Max Sustained Wind”:
max_sustained_winds[index], “Areas Affected”: areas_affected[index], “Damage”: updated_damages[index], “Deaths”: deaths[index]}}

and access the list stored as the value in the key “Areas Affected” and then take each area affected that is listed eg [“Florida”, “Cuba”, “Jamica”] and turn each of those into an individual key with an initial value of 1 in a new dictionary and then add 1 to the value each time a key from the new dictionary appears in the source dictionary.

I understand the theory of using a for loop to iterate through the source dictionary to access the information I eant but have been drawing a blank on how to implement it for this application

I underst

Hello @valaranin, welcome to the forums! Try doing it by hand. (Or at least one of them). Then try writing out instructions for how to do it by hand. Then, try turning those instructions into code. This is a very good way to approach any project that you can’t solve immediately.


Thanks for the advice after breaking it down and working through the steps, I came up with the following function:

Which gets the list of Areas Affected by iterating through key value in the argument dictionary, saves it to the variable lst and then iterates through lst adding it to the count_dictionary, dictionary if not already present or increasing the value by 1 if it is.

def affected_areas_count(dictionary):
  count_dictionary = {}
  lst = []
  for key,value in dictionary.items():
    lst = dictionary[key]["Areas Affected"]
    for location in lst:
      if location not in count_dictionary:
        count_dictionary[location] = 1
        count_dictionary[location] += 1

It seems to work in the project code panel and give what looks like the right output. Is there a better or more efficent way to do this?

1 Like

Ugh the indentation isnt displaying when I post for some reason

Please read ->this<- post to see how to correctly format your code in posts.

As for the code, it seem quite good. Without playing with the project, there’s nothing obvious that jumps out at me.

Thanks again for the help