US Medical Insurance Cost - Portfolio Project

Hello there!

I am stuck with my project about the US Medical Insurance Costs Portfolio, and I need your help to go ahead in my scope.

I considered 3 variables for my analysis:
- ages
- smokers
- regions
I created a list of lists made by the 3 variables called ages_smokes_regions with the following aspect:

[(19, 'yes', 'southwest'), (18, 'no', 'southeast'), (28, 'no', 'southeast'), (33, 'no', 'northwest'), (32, 'no', 'northwest'), ... , ]

I want to extract a new list from this one, containing only the record with smoking status = yes and age <= 21 years.

How you suggest to do?

I tried this nested loop:

ages_smokers_regions_new = []

for record in ages_smokers_regions:
    for e1, e2 in record: 
        if e1 <= 21 and e2 == 'yes':
            ages_smokers_regions_new.append(record)

but I get this type error message:

TypeError                                 Traceback (most recent call last)
<ipython-input-94-ecfd3a775a3f> in <module>
      2 
      3 for record in ages_smokers_regions:
----> 4     for e1, e2 in record:
      5         if e1 <= 21 and e2 == 'yes':
      6             ages_smokers_regions_new.append(record)

TypeError: cannot unpack non-iterable int object

Thank you for you help!!

You have to access your values first:

for record in ages_smokers_regions:
    if record[0] <= 21 and record[2] == 'yes':
        ages_smokers_regions_new.append(record)

If you wanted to unpack the records first, you could write:

for e1, e2, e3 in ages_smokers_regions:
    if e1 <= 21 and e2 == 'yes':
        ages_smokers_regions_new.append((e1, e2, e3))

… but this seems like an approach for a different problem, I personally wouldn’t do it this way. It’s less readable and doesn’t seem very scalable if you add more details.

1 Like

Thank you for your feedback.
No way to fix it.

I share my entire code here:

import csv

ages = []
with open("insurance.csv", newline = "") as age_data:
    age_reader = csv.DictReader(age_data, delimiter = ",")
    for row in age_reader:
        ages.append(row["age"])

ages = [int(i) for i in ages] 

smokers = []
with open("insurance.csv", newline = "") as smoker_data:
    smoker_reader = csv.DictReader(smoker_data, delimiter = ",")
    for row in smoker_reader:
        smokers.append(row["smoker"])

regions = []
with open("insurance.csv", newline = "") as region_data:
    region_reader = csv.DictReader(region_data, delimiter = ",")
    for row in region_reader:
        regions.append(row["region"])

one_list = zip(ages, smokers, regions)
ages_smokers_regions = list(one_list)
print(ages_smokers_regions)

ages_smokers_regions_new = []
for record in ages_smokers_regions:
    for e1, e2 in record: 
        if e1 <= 21 and e2 == 'yes':
            ages_smokers_regions_new.append(record)

Any further help will be very appreciated
thanks

Should work! Mind you when I gave the suggestion I wasn’t looking to fix my own errors but my suggestion:

for record in ages_smokers_regions:
    if record[0] <= 21 and record[2] == 'yes':
        ages_smokers_regions_new.append(record)

should be

for record in ages_smokers_regions:
    if record[0] <= 21 and record[1] == 'yes':
        ages_smokers_regions_new.append(record)

A quick print line like print(record[2]) would have highlighted that, since it was running without errors. Print statements are a wonderful (and humbling) debugging tool :slight_smile:

It worked! Thank you very much toastespitabread!! bless you… :smiley:

1 Like