Working with Python Lists: Medical Insurance Project

Hey Everyone!
I just finished working with Loops in Python. I modified the project at the very end to provide more specific information on the price for insurance per person. If there is any feedback that could have made this code more concise it would be much appreciated!
Thanks!
Happy Thanksgiving!

Not sure this count of feedback (I generally skip this kind of questions), but python has a built-in zip function to loop over multiple lists at the same time:

for (name, insurance_cost) in zip(names, actual_insurance_costs):

which means you can get rid of this:

  name = names[i]
  insurance_cost = actual_insurance_costs[i]

Not sure if functions have been taught yet? If they have, I would write a generic function for calculating the average of a list (if such a function doesn’t already exists, given this quite a common operation)

hope the feedback is somewhat useful :slight_smile:

In addition to the zip function mentioned above, you might want to take a look at something called f-string. While this particular method is not covered by Codecademy, if you are looking to make your code more concise it might go a long way in doing just that. For example, by using f-string, your first print statement can be re-written like so:

print(f"The Average Insurance Cost: {average_cost} dollars.")

Here is the good Real Python article on f-string to get you started: Python 3’s f-Strings: An Improved String Formatting Syntax (Guide)

1 Like