Python Strings: Medical Insurance Project Task 13 Missing Outputs

In tasks 9 through 13 (https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-strings/projects/ds-python-strings-project), you are required to create a new list of lists, stripped of all whitespaces. I have input the code as hinted, and from what I can see, the same as how every other person who has submitted their code on the forum for this project has.

My code, shown here

medical_data = """Marina Allison ,27 , 31.1 , #7010.0 ;Markus Valdez , 30, 22.4, #4050.0 ;Connie Ballard ,43 , 25.3 , #12060.0 ;Darnell Weber , 35 , 20.6 , #7500.0; Sylvie Charles ,22, 22.1 ,#3022.0 ; Vinay Padilla,24, 26.9 ,#4620.0 ;Meredith Santiago, 51 , 29.3 ,#16330.0; Andre Mccarty, 19,22.7 , #2900.0 ; Lorena Hodson ,65, 33.1 , #19370.0; Isaac Vu ,34, 24.8, #7045.0""" # Add your code here print(medical_data) umd = medical_data.replace("#", "$") print(umd) num_records = 0 for characters in umd: if characters == "$": num_records += 1 print("There are " + str(num_records) + " medical records in the data.") medical_data_split = umd.split(";") print("This is the medical data split " + str(medical_data_split)) medical_records = [] for entry in medical_data_split: medical_records.append(entry.split(",")) print("These are the medical records " + str(medical_records)) medical_records_clean = [] for record in medical_records: record_clean = [] for item in record: record_clean.append(item.strip()) medical_records_clean.append(record_clean) print("The cleaned medical records list is here " + str(medical_records_clean))

The output for the list of stripped lists (the final lines of code shown), only has the final record i.e.
[[‘Isaac Vu’, ‘34’, ‘24.8’, ‘$7045.0’]]

The rest of the records aren’t there. The hint for task 13 just shows what the output should look like, which contains all the other records in a list as well. I’m not sure why all the other outputs up until that point contain every other record, but this one doesn’t. I think it is something to do with the indexing, but I’m not really sure. Maybe something like each item in the sublists being indexed (e.g. BMI, age etc.) rather than the indexing referring to each sublist within the list.

Check the flow of your nested loops, what happens on every new iteration of medical_records?

If you’re not sure you can start some basic debugging by adding some print statements in to see what your objects contain at any point in your code. If your final list is not what you expect then that might be a good one to look at.

1 Like

Hmmm so I think I’ve figured out the issue by putting print statements almost everywhere. So the nested for loop strips each item (the string for the persons name, their BMI etc.) from each record (each individuals details). Once it has stripped and appended every item from one record to the list record_clean it then begins again, however, instead of adding the new items on to the end of the list of the previous record, it replaces them. I’m not sure why it does this.

When I move the second append statement at line 65 (that appends the record_clean list to the medical_record_clean list), to be indented the same as the nested for loop (on line 64, aligned with the above for statement), it then seems to fix this. I’m guessing that after each loop where each item in a record has been stripped and appended to record_clean, the loop then appends the complete record-clean to the medical_record_clean list, then begins replacing each item in record_clean with the new records items.

I’m hoping I’m on the right track, though the appended items in record_clean being overwritten each time it goes to a new record is strange to me.

1 Like

Yeah you got it, if it’s not clear why it was being cleared then these two lines sum it up-

for record in medical_records:
    record_clean = []

Each iteration an entirely new list is created, this is fine. Having a new list for each record is perfectly reasonable. But since you create a new list every time what do you then do with the list you just populated?

1 Like