Hi guys, I’m using the following code to create a new dictionary with years as the key. But when I run the code, it doesn’t show desired output. Notice that the years list has keys with duplicate values (1332 appears twice)

def updating_dict_by_years(names, month, year, max_sustained_wind, areas_affected, damage, death):

hurricane = {}

hurricane_len = len(year)

for i in range(hurricane_len):

hurricane[year[i]] = {"Name": names[i], "Month": month[i], "Year": year[i], "Max Sustained Wind": max_sustained_winds[i], "Areas Affected": areas_affected[i], "Damage": damage[i], "Death": death[i]}

return hurricane

test_dict_by_years = updating_dict_by_years(names, months, years, max_sustained_winds, areas_affected, damages, deaths)


how to return the first value that appears twice in years list as key? thanks~

Could you please format code in your future posts, see the following link for guidance- How do I format code in my posts? as it’s hard for others to read otherwise.

Each key in a dictionary needs to be unique so you can’t add two values with the same key (the one you add second would just remove the first from the dictionary).

I believe this instruction suggests using lists as the data type for each key. Each year (the keys of the main dictionary) should return a list containing one or more hurricanes (this bit would still be an dictionary) that were recorded that year.

Try adapting your code to that set-up using lists.