Python Lists: Medical Insurance Extra Task Help

https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-lists/projects/ds-python-lists1-project

I was wondering if it was possible to get the difference between two lists used in this project by subtracting the insurance cost list from the estimated insurance cost list.
E.g. List1 has the integers [1, 2, 3] and List2 has the integers [4, 5, 6], and you want to subtract the integers from List1 off of List2 row by row (4-1, 5-2, 6-3), resulting in a output of a list [3, 3, 3]. Something like List3 = List2 - List1 (I realise this example wouldn’t work).

yea, that is possible. But this would involve looping over the lists, subtracting the values and append the result of your subtraction to a new list.

1 Like

Thanks. So you couldn’t achieve the same result with the material covered so far in the Data Science coarse for Python? Doing it that way seemed the most efficient way of getting the difference, especially if you have to update the estimated and actual insurance cost lists regularly (rather than manually type in the new values and formula each time you need to add a persons data).

I haven’t done the data science course, so I don’t know. But I assume .append(), list and subtracting has been covered at this point. So you have all the pieces you need. Putting these pieces together is the challenge

which way? what you attempted? list2 - list1? Except it doesn’t work.

Maybe you can optimize the code further? you could also update the difference list every time you add an element, so you don’t have to create a new list every time.

1 Like

Sorry for the lack of clarity. I meant that by coding it so that the difference list is the estimated cost minus the actual cost (for each row of data, however you’d code that) seemed more efficient. That way, when the estimated and actual cost list are updated with a new persons data, you don’t have to add the new data to the lines of code for the difference list (it would just calculate the difference for however many rows there are in the estimated and actual cost lists).

I hadn’t figured out if there was a way to do this with the material I had covered so far. But I was trying to think of a way of being able to make a difference list that doesn’t need to be updated each time new data is added to the estimated or actual cost lists. It seemed better to have it take the difference between the two lists row by row (and not having to manually type in the data from each row of each list into the code for the difference list). It seemed like a very slow, and inefficient solution if I had to type it in like this
list3 = [4-1, 5-2, 6-3] and so on for every new value that is added.

I lost track, I have no idea what solution you currently implemented or what where ideas you discarded.

How do you know calculate the difference between estimated and actual cost?

Sorry on the delayed response and confusion, it was late at night when I was responding and I hadn’t figured out exactly how to code the solution I wanted. I finally figured out how to code the solution I wanted by scrolling through a few forums. The code given below is what I wanted to do. It contains a for loop, which I haven’t covered in my course yet (it’s coming up soon though). Using the code below, every time I append new elements into the insurance_costs list, and the estimated_costs lists, the insurance_cost_difference list automatically updates.

insurance_costs = [4150.0, 5320.0, 35210.0, 2149.0]
estimated_costs = [4222.0, 5442.0, 36368.0, 2149.0]

def insurance_cost_difference(estimated_costs, insurance_costs):
  difference = [estimated_costs[x] - insurance_costs[x]
  for x in range(len(estimated_costs))]
  return difference

print(insurance_cost_difference(estimated_costs, insurance_costs))

Whereas the way I originally coded the solution was by subtracting each insurance_cost value from each estimated_cost value manually as seen below.

insurance_cost_difference = [("Maria", 4222.0 - 4150.0), ("Rohan", 5442.0 - 5320.0), ("Valentina", 36368.0 - 35210.0), ("Akira", 2149.0-2149.0)]
print(insurance_cost_difference)

I think that is the way the project would have you code it.

when for loop hasn’t been covered yet, then this extra challenge becomes very daunting.

Seems you got covered now :slight_smile:

1 Like