Finding difference in 2d list

  • 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.
insurance_costs = [('Maria', 4150.0), ('Rohan', 5320.0), ('Valentina', 35210.0)]
estimated_insurance_data = [('Maria', 4222.0), ('Rohan', 5442.0), ('Valentina', 36368.0)]

If you don’t want to use loops, I guess you could do:

insurance_cost_difference = []
insurance_cost_difference.append( (insurance_costs[0][0], insurance_costs[0][1] - estimated_insurance_data[0][1]) )
insurance_cost_difference.append( (insurance_costs[1][0], insurance_costs[1][1] - estimated_insurance_data[1][1]) )
insurance_cost_difference.append( (insurance_costs[2][0], insurance_costs[2][1] - estimated_insurance_data[2][1]) )
print(insurance_cost_difference)

Notice that this is a tuple in a loop.
insurance_cost[0] is ('Maria', 4150.0)
and in that tuple, [0] is 'Maria' and [1] is 4150.0
so insurance_cost[0][0] is 'Maria'
so insurance_cost[0][1] is 4150.0