Getting a “list index out of range” error

Ok so I was adjusting and now I’m getting a “list index out of range” error but when I change it back its fine.

Here’s my updated code: This code doesn’t work:

def hurricanes():
    nlookup = {}                                    
    for i in range(len(names)):                     
        data = {}                                   
        data['name'] = names[i]                     
        data['month'] = months[i]                   
        data['year'] = years[i]                     
        data['max wind'] = max_sustained_winds[i]   
        data['areas affected'] = areas_affected[i]  
        data['damages'] = updated_damages[i]        
        data['deaths'] = deaths[i]                  
        nlookup[names[i]] = data                    
    return nlookup                                  

nlookup = hurricanes()        

here’s my original code: This code works:

def create_dictionary():
    hurricanes = {}                                 # creates an empty dictionary, in the scope of the create_dictionary function
    for i in range(len(names)):                     # for each index in the range of the list names
        data = {}                                   # create an empty dictionary for the data
        data['Name'] = names[i]                     # in the dictionary data set 'Name' = to the name at the current index i (in this case) in the range
        data['Month'] = months[i]                   # in the dictionary data set 'Month' = to the month at the current index in the range
        data['Year'] = years[i]                     # in the dictionary data set 'Year' = to the year at the current index in the range
        data['Max Wind'] = max_sustained_winds[i]   # in the dictionary data set 'Max Wind' = to the max sustained wind at the current index in the range
        data['Areas Affected'] = areas_affected[i]  # in the dictionary data set 'Areas Affected' = to the area affected at the current index in the range
        data['Damages'] = updated_damages[i]        # in the dictionary data set 'Damages' = to the damages at the current index in the range
        data['Deaths'] = deaths[i]                  # in the dictionary data set 'Deaths' = to the number of deaths at the current index in the range
        hurricanes[names[i]] = data                 # in the empty dictionary hurricanes set each name at the current index in the range of the list of names = to the new dictionary called data
    return hurricanes                               # return the dictionary hurricanes

Bear in mind where we step in…


dlu = []
nlu = {}
ylu = {}
mlu = {}
alu = {}

kys = ['name', 'month', 'year', 'max_sustained_winds', 'areas_affected', 'damages', 'deaths']

db = zip(names, months, years, max_sustained_winds, areas_affected, damages, deaths)

The named references at that point are the seven lists. We help perpetuate their names by making them keys (singular in some cases) right off the bat.

I guess I’m not following

That’s okay. We’re pretty much in the deep end so feel proud that you’re treading water. Did I link to my workspace in this thread? There is a proof of concept in terms of working from two types of data stores. Don’t want to link again if you already have it.

1 Like

Above my hurricanes function as:

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']

That’s not a function, it’s a list. Surely there is more?

Sorry here is the code I have that is causing the error:

# 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]

# function for converting damages strings to floats:
updated_damages = []

def damage_conversion(list_of_damages):
    for damage_value in list_of_damages:
        if damage_value == 'Damages not recorded':
            updated_damages.append('Damages not recorded')
        elif damage_value[-1] == 'B':
            billion_conversion = float(damage_value[:-1]) * 1000000000
            updated_damages.append(billion_conversion)
        elif damage_value[-1] == 'M':
            million_conversion = float(damage_value[:-1]) * 1000000
            updated_damages.append(million_conversion)
    return updated_damages
        
#print(damage_conversion(damages), '\n')


# hurricane dictionary function:
def hurricanes():
    nlookup = {}                                    # creates an empty dictionary, in the scope of the hurricanes function: nlookup for name look up, dictionary sorted by names
    for i in range(len(names)):                     # for each index in the range of the list names
        data = {}                                   # create an empty dictionary for the data
        data['name'] = names[i]                     # in the dictionary data set 'name' = to the name at the current index i (in this case) in the range
        data['month'] = months[i]                   # in the dictionary data set 'month' = to the month at the current index in the range
        data['year'] = years[i]                     # in the dictionary data set 'year' = to the year at the current index in the range
        data['max wind'] = max_sustained_winds[i]   # in the dictionary data set 'max wind' = to the max sustained wind at the current index in the range
        data['areas affected'] = areas_affected[i]  # in the dictionary data set 'areas affected' = to the area affected at the current index in the range
        data['damages'] = updated_damages[i]        # in the dictionary data set 'damages' = to the damages at the current index in the range
        data['deaths'] = deaths[i]                  # in the dictionary data set 'deaths' = to the number of deaths at the current index in the range
        nlookup[names[i]] = data                    # in the empty dictionary nlookup set each name at the current index in the range of the list of names = to the new dictionary called data
    return nlookup                                  # return the dictionary nlookup

nlookup = hurricanes()   

my error is at the line:

 data['damages'] = updated_damages[i]  

That is a variable in another function. It does not exist in present scope.

1 Like

Ah duh! I didn’t call that function so it was empty

1 Like

https://www.codecademy.com/workspaces/61bea3828f5e57a8e8521429

1 Like

Of course that was complete BS about keys and capitalization. Well, not complete, just not lenient enough. When the keys come from our data, we don’t change letter case. At least we wouldn’t until it was vetted and could be cross-referenced if needs be. We have zoodles of capitalized keys, here. The ones created by the author are, you guessed it, lower case.

This error basically means you are trying to access a value at a List index which is out of bounds i.e greater than the last index of the list or less than the least index in the list. So the first element is 0, second is 1, so on. So if there are n elements in a python list, the last element is n-1 . If you try to access the empty or None element by pointing available index of the list, then you will get the "List index out of range " error. To solve this error, you should make sure that you’re not trying to access a non-existent item in a list.

An index in Python refers to a position within an ordered list . To retrieve an element of the list, you use the index operator ([]) . Using indexing you can easily get any element by its position.