MemoryError in python dictionaries challenge projects [solved - not casting str to float cause it to multiply by 10^6]

Hi all,

I’m starting now the python dictionaries challenge projects: hurricane analysis and I can’t even seem to run the first task.

Basically I have to run through the damages list that has 3 different types of values: ##.#B, ##.#M and ‘Damages not recorded’.

The task is to take the list and turn it into a numeral list of damages, where B = 1000000000 and M = 1000000 in USD$.

But as soon as I try to run through the list I get memory error on Jupyter or endless spinning on Codecademy platform.

The code:

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

conversion = {"M": 1000000,

              "B": 1000000000}

def updated_damages(damages_list):
  new_list = []
  for item in damages_list:
    if item == 'Damages not recorded':
      new_list.append(item)
    else:
      print(item[:-1])
      print(conversion[item[-1]]) # is this bugging out?
      #NOT WORKING AT ALL: new_list.append(item[:-1] * conversion[item[-1]])
  return new_list
# test function by updating damages

updated_damages_list=updated_damages(damages)

print(updated_damages_list)

MemoryError:
image

What am I doing wrong here?
I have a hunch about conversion[item[-1]] but not seeing what’s wrong with it.

Thanks!

Cannot see where that is appended.

It is commented out.
But it doesn’t really matters, even the print get the memory error and crashing

The multiplication is actually repeat. Be sure to convert to int.

2 Likes

Of course! How silly of me. Thank you!

2 Likes