How to iterate through a list while separating the items within the list

Link to the Hurricane Analysis:

For the given task to separate the items within the list, I seem to be having trouble understanding how to filter through the items. The first part, from my understanding, should separate any of the strings in the ‘damages’ list that contains a number with either an ‘M’ or ‘B’. However, when the code runs, the output just splits every character. My question is about whether the logic is wrong to start this task or if my code is incorrectly attempting to find the items with ‘M’ or ‘B’?

# Updating damages list
# Split the input into amount & rate based on if-elif-else statement(s)
# Create an empty list to concatenate all the elements in damages
new_list = []
    # Add all elements in there
for item in damages:
  if (item == item.find("M")) or (item == item.find("B")):
    new_list += item
    'Damages not recorded'
    new_list += item  

Thank you in advance for your help

Good start…but, the .split() method is a string method and will split a string into a list of strings, which is why your result is split into each character from the damages list.


The directions ask to write a function for this step. Can you think of another way to use their provided:
conversion = {"M": 1000000, "B": 1000000000}

and iterate through the damages list to find the said items and convert them into a new list?
Here’s the beginning of one possible way for example:

def updated_damages_data(damages):
conversion = {"M": 1000000,
               "B": 1000000000}

  updated_damages = list()
for damage in damages:
    if damage == "Damages not recorded":

1 Like

Thank you, one question though. Why move the conversion dictionary into the function? From what I understand, the dictionary should be accessible to anything when it’s not contained in a function. Thank you again.

Good question. The dictionary is just the conversion that you use to multiply what you find; if you find “M”, then you use that key’s value to multiply it by . If you have another damages list that you need converted, then you’d pass that through the function and it’d be converted using that dictionary’s values. You could also update that conversion dictionary to include maybe trillions or what have you. (Though, it’d be pretty awful to have a hurricane that terrible. Yikes).

*Also, I had sort of a Ted Lasso moment when I looked at the word “damages” and thought it was spelled wrong. :joy:

1 Like