I DID IT! I would really appreciate some eyes_on/feedback_for my project looking at U.S. Med!

Hey people, I made something WOHOOO!

It might be a little clunky, but I wanted o check out ave BMIs split along “sex” and whether or not the people had children.

I feel like going forward, looking at insurance costs by “sex” and children could be helpful in contributing to the reproductive rights conversation.

Hey there, I liked what type of questions you were trying to answer and that you were able to calculate statistics to figure out some of those questions.
I did have some tips that might help with your code:

I noticed you made a dictionary of all the patients. The initial part of the code with the DictReader creates a dictionary of all the entries in the csv file so in order to use it you can .append it to a new list like so:
import csv

with open('insurance.csv') as insurance_csv:
    insurance_reader = csv.DictReader(insurance_csv)
    new_list = []
    for row in insurance_reader:
        new_list.append(row)
print(new_list[0:4])

the last line prints the first 5 dictionaries in the list which look like this:
[{'age': '19', 'sex': 'female', 'bmi': '27.9', 'children': '0', 'smoker': 'yes', 'region': 'southwest', 'charges': '16884.924'}, {'age': '18', 'sex': 'male', 'bmi': '33.77', 'children': '1', 'smoker': 'no', 'region': 'southeast', 'charges': '1725.5523'}, {'age': '28', 'sex': 'male', 'bmi': '33', 'children': '3', 'smoker': 'no', 'region': 'southeast', 'charges': '4449.462'}, {'age': '33', 'sex': 'male', 'bmi': '22.705', 'children': '0', 'smoker': 'no', 'region': 'northwest', 'charges': '21984.47061'}]

I also noticed in the area where you calculate average BMI for each sex that you have a bunch of code in different cells. Calculating an average is a great opportunity to bundle that up into a function which could take in a list as a parameter and execute all your code to get an average. For example something like this:

def average_for_sexes(info_list):
    new_list = zip(sex_list, info_list)
    total_bmi_m = 0
    total_bmi_w = 0
    total_m = 0
    total_w = 0
    for person in new_list:
        if person[0] == 'female':
          etc.....

I also think that instead of creating 4 variables to keep track of numbers, it might be easier to create 2 lists instead. You could append each number to a “male” and a “female” list then get the average by doing sum and divide by length of list. Here is an example:

def average_for_sexes(info_list, trait):
    new_list = zip(sex_list, info_list)
    female_list = []
    male_list = []
    for person in new_list:
        if person[0] == 'female':
            female_list.append(person[1])
        else:
            male_list.append(person[1])
    avg_female = round(sum(female_list)/len(female_list),2)
    avg_male = round(sum(male_list)/len(male_list),2)
    print(
    """The average female {trait} is {} 
    compared to the average male {trait} 
    of {}""".format(avg_female, avg_male)

Great job, and good luck on your coding journey!

1 Like

Thank you so much, Jeff!

I was having trouble conceptualizing making a dictionary without indexes. You way is WAY cleaner! I also like the idea of creating a function for finding the average since my code is long-winded and repeating. One question. In this code:

def average_for_sexes(info_list, trait):
new_list = zip(sex_list, info_list)
female_list =
male_list =
for person in new_list:
if person[0] == ‘female’:
female_list.append(person[1])
else:
male_list.append(person[1])

I have a question about the bolded index. Why is it 1 and not 0?
I really appreciate the time you put into this! You not only offered some stellar code, but explained it so I didn’t feel like a goof!

Hey there, the part with the bolded index came from your original code. I think because you zip a list, the index 0 is where you find the sex and index 1 is where the BMI is stored (or in this function, where the value of the other list is stored).

Ahh, thanks again, Jeff!