Says index is out of range, but list being called is same length?

  1. Additional data collected on the 34 strongest Atlantic hurricanes are provided in a series of lists. The data includes:
  • names: names of the hurricanes
  • months: months in which the hurricanes occurred
  • years: years in which the hurricanes occurred
  • max_sustained_winds: maximum sustained winds (miles per hour) of the hurricanes
  • areas_affected: list of different areas affected by each of the hurricanes
  • deaths: total number of deaths caused by each of the hurricanesThe data is organized such that the data at each index, from 0 to 33, corresponds to the same hurricane.For example, names[0] yields the “Cuba I” hurricane, which occurred in months[0] (October) years[0] (1924).Write a function that constructs a dictionary made out of the lists, where the keys of the dictionary are the names of the hurricanes, and the values are dictionaries themselves containing a key for each piece of data (Name, Month, Year, Max Sustained Wind, Areas Affected, Damage, Death) about the hurricane.Thus the key "Cuba I" would have the value: {'Name': 'Cuba I', 'Month': 'October', 'Year': 1924, 'Max Sustained Wind': 165, 'Areas Affected': ['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], 'Damage': 'Damages not recorded', 'Deaths': 90}.Test your function on the lists of data provided.
# names of hurricanes
names = ['Cuba I', 'San Felipe II Okeechobee', 'Bahamas', 'Cuba II', 'CubaBrownsville', 'Tampico', 'Labor Day', 'New England', 'Carol', 'Janet', 'Carla', 'Hattie', 'Beulah', 'Camille', 'Edith', 'Anita', 'David', 'Allen', 'Gilbert', 'Hugo', 'Andrew', 'Mitch', 'Isabel', 'Ivan', 'Emily', 'Katrina', 'Rita', 'Wilma', 'Dean', 'Felix', 'Matthew', 'Irma', 'Maria', 'Michael']

# months of hurricanes
months = ['October', 'September', 'September', 'November', 'August', 'September', 'September', 'September', 'September', 'September', 'September', 'October', 'September', 'August', 'September', 'September', 'August', 'August', 'September', 'September', 'August', 'October', 'September', 'September', 'July', 'August', 'September', 'October', 'August', 'September', 'October', 'September', 'September', 'October']

# years of hurricanes
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]

# maximum sustained winds (mph) of hurricanes
max_sustained_winds = [165, 160, 160, 175, 160, 160, 185, 160, 160, 175, 175, 160, 160, 175, 160, 175, 175, 190, 185, 160, 175, 180, 165, 165, 160, 175, 180, 185, 175, 175, 165, 180, 175, 160]

# areas affected by each hurricane
areas_affected = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States'], ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], ['The Bahamas', 'Cuba', 'Florida', 'Texas', 'Tamaulipas'], ['Jamaica', 'Yucatn Peninsula'], ['The Bahamas', 'Florida', 'Georgia', 'The Carolinas', 'Virginia'], ['Southeastern United States', 'Northeastern United States', 'Southwestern Quebec'], ['Bermuda', 'New England', 'Atlantic Canada'], ['Lesser Antilles', 'Central America'], ['Texas', 'Louisiana', 'Midwestern United States'], ['Central America'], ['The Caribbean', 'Mexico', 'Texas'], ['Cuba', 'United States Gulf Coast'], ['The Caribbean', 'Central America', 'Mexico', 'United States Gulf Coast'], ['Mexico'], ['The Caribbean', 'United States East coast'], ['The Caribbean', 'Yucatn Peninsula', 'Mexico', 'South Texas'], ['Jamaica', 'Venezuela', 'Central America', 'Hispaniola', 'Mexico'], ['The Caribbean', 'United States East Coast'], ['The Bahamas', 'Florida', 'United States Gulf Coast'], ['Central America', 'Yucatn Peninsula', 'South Florida'], ['Greater Antilles', 'Bahamas', 'Eastern United States', 'Ontario'], ['The Caribbean', 'Venezuela', 'United States Gulf Coast'], ['Windward Islands', 'Jamaica', 'Mexico', 'Texas'], ['Bahamas', 'United States Gulf Coast'], ['Cuba', 'United States Gulf Coast'], ['Greater Antilles', 'Central America', 'Florida'], ['The Caribbean', 'Central America'], ['Nicaragua', 'Honduras'], ['Antilles', 'Venezuela', 'Colombia', 'United States East Coast', 'Atlantic Canada'], ['Cape Verde', 'The Caribbean', 'British Virgin Islands', 'U.S. Virgin Islands', 'Cuba', 'Florida'], ['Lesser Antilles', 'Virgin Islands', 'Puerto Rico', 'Dominican Republic', 'Turks and Caicos Islands'], ['Central America', 'United States Gulf Coast (especially Florida Panhandle)']]

# damages (USD($)) of hurricanes
damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M', '27.9M', '5M', 'Damages not recorded', '306M', '2M', '65.8M', '326M', '60.3M', '208M', '1.42B', '25.4M', 'Damages not recorded', '1.54B', '1.24B', '7.1B', '10B', '26.5B', '6.2B', '5.37B', '23.3B', '1.01B', '125B', '12B', '29.4B', '1.76B', '720M', '15.1B', '64.8B', '91.6B', '25.1B']

# deaths for each hurricane
deaths = [90,4000,16,3103,179,184,408,682,5,1023,43,319,688,259,37,11,2068,269,318,107,65,19325,51,124,17,1836,125,87,45,133,603,138,3057,74]

# 2
# Create a Table

def hurricane_info_dictionary(names, months, years, max_sustained_winds, areas_affected, damages, deaths):
    for i in range(34):
        keys = []
        keys.append(names[i])
        values = []
        value_creator = {'Name': names[i], 'Month': months[i], 'Year': years[i], 'Max Sustained Wind': max_sustained_winds[i], 'Areas Affected': areas_affected[i], 'Damage': damages[i], 'Deaths': deaths[i]}
        values.append(value_creator)
        hurricane_info = {keys[i]: values[i]}
    print(hurricane_info)
hurricane_info_dictionary(names, months, years, max_sustained_winds, areas_affected, damages, deaths)


Output:

IndexError Traceback (most recent call last)
/var/folders/zn/3p1r8mqj4xs4_kgjm6k49vlw0000gn/T/ipykernel_31529/535132911.py in
32 hurricane_info = {keys[i]: values[i]}
33 print(hurricane_info)
—> 34 hurricane_info_dictionary(names, months, years, max_sustained_winds, areas_affected, damages, deaths)

/var/folders/zn/3p1r8mqj4xs4_kgjm6k49vlw0000gn/T/ipykernel_31529/535132911.py in hurricane_info_dictionary(names, months, years, max_sustained_winds, areas_affected, damages, deaths)
30 value_creator = {‘Name’: names[i], ‘Month’: months[i], ‘Year’: years[i], ‘Max Sustained Wind’: max_sustained_winds[i], ‘Areas Affected’: areas_affected[i], ‘Damage’: damages[i], ‘Deaths’: deaths[i]}
31 values.append(value_creator)
—> 32 hurricane_info = {keys[i]: values[i]}
33 print(hurricane_info)
34 hurricane_info_dictionary(names, months, years, max_sustained_winds, areas_affected, damages, deaths)

IndexError: list index out of range

How is the hurricane_info dictionary index out range when I’m just calling items from list with the same length??

THANK YOU

You can try implementing this, it worked for me:
def hurricane_info_dictionary(names, months, years, max_sustained_winds, areas_affected, damages, deaths):
hurricane_info = {}
for i in range(34):
key = names[i]
value = {‘Name’: names[i], ‘Month’: months[i], ‘Year’: years[i], ‘Max Sustained Wind’: max_sustained_winds[i], ‘Areas Affected’: areas_affected[i], ‘Damage’: damages[i], ‘Deaths’: deaths[i]}
hurricane_info[key] = value
print(hurricane_info)
hurricane_info_dictionary(names, months, years, max_sustained_winds, areas_affected, damages, deaths)

You have keys = [] and values = [] inside the loop, so you’re resetting each of these lists to be an empty list on each iteration of the loop (so the highest index for these would be 1).
To fix that, put keys = [] and values = [] before the loop.

Similarly, you’re creating a new hurricane_info dictionary on each iteration of the loop, instead of adding to an existing dictionary in each iteration.

Also, the function currently does not return anything.

ahhhhhh yes of course. hindsight is always 20/20 isn’t it? Thank you so much!