Coding Challenge: Hurricane Analysis

Hi, im currently working on the coding practice project, section 3, found here: https://www.codecademy.com/practice/projects/hurricane-analysis

I have defined a function, which takes a number of perimeters, all of which are lists. My goal is to make a single dictionary, which combines an item from each of the list, each at a particular index, together into a single dictionary. What i have provided below produces no errors, but it seems to be erasing each entry as it adds another, so that all i am left with is a single dictionary entry with all the information at the end of my range, in this case, 34. How do i make my output, new_dict, is a dictionary of 34 dictionaries? Why is my .update erasing the previous entry each time a new entry is added?

thanks in advance

def hurricane_zip(names, months, years, max_sustained_winds, areas_affected, new_damages, deaths):
  new_dict = {}
  new_zip = {}
  for index in range(0,34):
    new_zip.update({"Name": names[index], "Month": months[index], "Year": years[index], "Max Sustained Wind": max_sustained_winds[index], "Areas Affected": areas_affected[index], "Damage": new_damages[index], "Deaths": deaths[index]})
    new_dict.update(new_zip)
  return new_dict
test = hurricane_zip(names, months, years, max_sustained_winds, areas_affected, new_damages, deaths)
print(test)

Hi @digital9310609615,

The update method of dict uses key/value pairs from the argument that is passed to it to either create new key/value pairs or to overwrite the values associated with existing keys within the dict from which it is called. During each iteration of the for loop, you are overwriting the data processed during the previous iteration, leaving only the information related to the final hurricane represented in the data after the loop terminates.

It may be more effective for you to retrieve keys from names list, and use the other lists to provide the values that are to be associated with the keys in the internal dictionaries that represent each hurricane. A line such as the following, placed inside the for loop, would accomplish that:

    new_dict[names[index]] = {"Name": names[index],
                              "Month": months[index],
                              "Year": years[index],
                              "Max Sustained Wind": max_sustained_winds[index],
                              "Areas Affected": areas_affected[index],
                              "Damage": new_damages[index],
                              "Death": deaths[index]}

3 Likes