Medical insurance project

I want to create dictionary that key is ‘smoking stauts’ value is ‘charges’. But my code print out dictionary only last two pairs. how to figure out . pls help me

import csv
ages = []
sex = []
bmi = []
children = []
smoker = []
region = []
charges = []

with open('insurance.csv', newline = '') as insurance_csv:
    
    reading_data = csv.DictReader(insurance_csv)
    for row in reading_data:
        ages.append(row['age'])
        sex.append(row['sex'])
        bmi.append(row['bmi'])
        children.append(row['children'])
        smoker.append(row['smoker'])
        region.append(row['region'])
        charges.append(row['charges'])
        
charge_dict = {smoker:charges for smoker,charges in zip(smoker, charges)}

{'yes': '29141.3603', 'no': '2007.945'}     

Remember that dictionary keys must be unique. If you’re trying to capture multiple values consider a different data type, e.g. have your key refer to a list object and append to it or something along those lines.

Might be worth adding a link to the project or some details about what the .csv file actually contains.

1 Like