How to calculate list differences

I am stuck! I wanted to calculate the difference between the actual insurance cost data and the estimated insurance cost data for each individual, and store the results in a list called insurance_cost_difference.

here is the data:

actual_insurance_cost_data = [(‘Maria’, 4150.0), (‘Rohan’, 5320.0), (‘Valentina’, 35210.0)]
estimated_insurance_cost_data: [(‘Maria’, 4222.0), (‘Rohan’, 5442.0), (‘Valentina’, 36368.0)]

Please help. What is the correct syntax?

Here is the project link, question #12:

1 Like

What have you tried so far? Do you have a basic logic worked out? If not start with pen and paper and consider how you could solve this problem without the script. Converting it into valid code after that is much simpler.

If you wanted to get the first cost value from actual_insurance_cost_data then you could address it in the following way: actual_insurance_cost_data[0][1] which is the first list element [0] and second element [1] in the tuple. If you used the same style of indexing to get the equivalent value of estimated_insurance_cost_data then you can start calculating the difference. If you can then take that logic and turn it into a loop and you’d be well on the way to a possible solution.

At present it’s a little trickier than it perhaps needs to be with nested structures. Consider whether these lists would be better off as dictionaries or similar.


I hope this one helps (I am abbreviating actual_insurance_cost_data as aicd and estimated_insurance_cost_data as eicd):

insurance_cost_difference = [(eicd[i], eicd[i] - aicd[i] for i in range(len(aicd))]

This is the shortest solution as I think.
P.S. you can replace the first occurrence of eicd with aicd and aicd in len() with eicd If and only if length of both the lists are same.

1 Like

This one took me a bit to figure out. I think part of it has to do with the exercise forcing you to use similar variable names that you better describe later - it just makes deciphering your previous code a little more difficult. Below is my markup with comments included. I put this at the very end, after completing all preceding steps, so it does rely on pulling from earlier points in the exercise. I also added in the names list so it was clear who each difference calculation was for. I checked the math and it does provide the correct answer, as to if this is the ‘right’ way, well ¯_(ツ)_/¯. After all, it works!

#Calculate Actual/Estimate Diff Problem

#Calculating the Differences

maria_diff = maria_insurance_cost - insurance_costs[0] #Maria's estimate in the list

rohan_diff = rohan_insurance_cost - insurance_costs[1] #Rohan's estimate in the list

valentina_diff = valentina_insurance_cost - insurance_costs[2] #Valentina's estimate in the list

#Putting Differences into a new List

insurance_cost_difference = [maria_diff, rohan_diff, valentina_diff]

#Zipping the Names with the Differences

diff_with_names = zip(names, insurance_cost_difference)

#Printing the Zipped List as a List


Hi @veysel_betul, just wanted to contribute for those who may come after us in this exercise. I was able to overcome the insurance cost difference with the code below.

insurance_cost_difference = insurance_data[0][1] - estimated_insurance_data[0][1],insurance_data[1][1]- estimated_insurance_data[1][1],insurance_data[2][1] - estimated_insurance_data[2][1]

insurance_cost_difference = list(zip(names,insurance_cost_difference))

print("\n The difference between the actual insurance cost data and the estimated insurance cost data for each individual is "+str(insurance_cost_difference))