Update keys in a dictionary with elements in a list

How can I update keys of a dictionary with elements of a list?
Here is a link to the challenge-----https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-dictionaries-challenge-projects/projects/hurricane-analysis

Welcome to the forums.

Is this Q3 from that challenge? (I can’t recall)
What code do you have so far for it? What have you tried?

Also, here’s a hint:

1 Like

I created a dictionary from here
updated_dic = {}

def updat(elem_1, elem_2, elem_3, elem_4, elem_5, elem_6, elem_7):

for i in range(len(years)):

    updated_dic.update({elem_1[i] : {'Name': elem_1[i], 'Month': elem_2[i], \

    'Year': elem_3[i], 'Max_Sustained_Wind': elem_4[i], 'Areas_Affected': elem_5[i], 'Damage': elem_6[i], 'Death': elem_7[i]}})

return updated_dic

updat(names, months, years, max_sustained_winds,areas_affected, updated_damages, deaths)


I’m challenged to update this dictionary but change the keys with elements of a list.
Here’s the list
years = [1924, 1928, 1932, 1932, 1933, 1933, 1935, 1938, 1953, 1955, 1961, 1961, 1967, 1969, 1971, 1977, 1979, 1980, 1988, 1989, 1992, 1998, 2003, 2004, 2005, 2005, 2005, 2005, 2007, 2007, 2016, 2017, 2017, 2018]
Here’s my code
hurricane_dic = {}

for k in years:

hurricane_dic.update({k : i for i, j in updated_dic.items()})

But it doesn’t solve the problem

Okay, so, you have to write another function that is based on that last dictionary you created of the hurricanes, you called it updated_dic which is the parameter in your new function.

Maybe we call it:

def year_dictionary(updated_dic):
  hurricane_years= dict()
  for cane in updated_dic:

You can create a variable inside that function and call it hurricanes_years (or whatever) and then set it equal to dict().

"The dict() constructor builds dictionaries directly from sequences of key-value pairs "(which you already have in your updated dictionary prior to it.

The hint is useful too.

See this:

1 Like

Thanks for the hints, helpful.


Not sure if I’m out in left field or not…

def convert_to_values():
  global damages
  x = damages
  for y in range(len(x)):
    if x[y] == 'Damages not recorded':
    x[y] = float(x[y][:-1]) * conversion.get(x[y][-1])

Hurricane Keys
hurricane_keys = {
  'Name': names,
  'Month': months,
  'Year': years,
  'Max Sustained  Wind': max_sustained_winds,
  'Areas Affected': areas_affected,
  'Damage': damages,
  'Deaths': deaths
Hurricanes by Name
hurricanes_by_name = {name: {} for name in names}
print (hurricanes_by_name)
for i, x in enumerate (hurricanes_by_name.items()):
  k, v = x
  for j, u in hurricane_keys.items():
    v[j] = u[i]
print (hurricanes_by_name['Katrina'])
Hurricanes by Year
hurricanes_by_year = {year: [] for year in set(years)}
print (hurricanes_by_year)
for i, x in enumerate(years):
  hurricanes_by_year[x].append({j:u[i] for j, u in hurricane_keys.items()})
print (hurricanes_by_year[2005])
Hurricanes by Area
flat = [y for x in areas_affected for y in x]
damaged_areas = {z:0 for z in set(flat)}
for x in flat:
  damaged_areas[x] += 1
print (damaged_areas['Florida'])

Is this helpful, or hindering?

Very Helpful. Thanks

1 Like

Did you make it through the rest? Or are you like me, taking it one step at a time with a long break between? Just now going into #8.

Seems only fair to keep letting you in on a tinkerer’s mind and share my thoughts on where we’re at.

Maximum Hurricane Count
max_key, max_value = 'Florida', damaged_areas['Florida']
for k, v in damaged_areas.items():
  if v > max_value:
    max_value = v
    max_key = k
print (f"Most affected: {max_key}, Frequency: {damaged_areas[max_key]}")
Deadliest Hurricane
max_deaths = max(deaths)
print (f"{names[deaths.index(max_deaths)]} {max_deaths}")
Rating Hurricanes by Mortality
def rating(x):
    return len([*filter(lambda y: x > y, [0, 100, 500, 1000, 10000])])

mort_ratings = zip(names, map(rating, deaths))
ratings = {r: [] for r in [0, 1, 2, 3, 4, 5]}
for x, y in mort_ratings:

Bear in mind that these solutions veer off from the instructions so need to be refined to fit the expected model. They are written to satisfy moi, and to get in some daily exercise.

1 Like

Yes, I’m through with the exercise. Just as you said, one at a time. Hectic for a beginner like me. Seeing your code, I feel bad cos yours is compact and efficient. Mine is not so clean as yours, though gets the job done. Would like to partner with you bro.

Is that really true, though? Is it more compact and efficient, or does it just look that way? Better to partner with someone who is at or near your level of learning so you can hash things out amongst you.

From me you would likely pick up bad habits since I generally go my own way in a effort to experiment with code patterns. I don’ t have a boss and no collaborators, or even any running projects. This is a hobby, and I tend to trivialize a lot of things. Take what you can from what you see, but don’t let that interfere with your learning path. Keep doing what you’re doing and in time you will discover or devise your own refinements.

True. Hope you don’t mind if I keep in touch anytime I run into a coding quagmire?

1 Like

That’s what we’re here for. Glad to help.