Python Strings: Medical Insurance Project

Hi there, I was doing a string exercise and the result was odd to me.
So this task requests me to separate the strings in a list through comma ( ‘,’).
It’s a nested list, and I try splitting and append them to a new list. The result turned out just fine.

medical_data_split =['Marina Allison ,27 , 31.1 , \n$7010.0 ', 'Markus Valdez , 30, \n22.4, $4050.0 ', 'Connie Ballard ,43 \n, 25.3 , $12060.0 ', ‘Darnell Weber \n, 35 , 20.6 , $7500.0’, '\nSylvie Charles ,22, 22.1 \n,$3022.0 ', ’ Vinay Padilla,24, \n26.9 ,$4620.0 ‘, ‘Meredith Santiago, 51 , \n29.3 ,$16330.0’, ’ Andre Mccarty, \n19,22.7 , $2900.0 ‘, ’ \nLorena Hodson ,65, 33.1 , $19370.0’, ’ \nIsaac Vu ,34, 24.8, $7045.0’]


for record in medical_data_split:

However as the project goes on, I got incorrect results. I reviewed all my step and found only the mentioned part is different from the official answer.

Hinted syntax:

for record in medical_data_split:

The official answer looks to me was only a concise version of what I wrote. Can anyone tell me what causes the difference? Thanks in advance!

You need to be careful with what the .split method actually does which is a good time to check the docs-

Notably it is a method that returns something. What do you do with the output of this method in the first example?

I was doing as told in the lecture. The project instruction is as follow:

Next, iterate through medical_data_split and for each record, split the string after each comma ( , ) and append the split string to medical_records .
Print medical_records after the loop.

It seems to me that the purpose of implementing .spilt method is to separate each data in the list and for further analytical needs.

It does do that, but it does not alter the existing string, it creates an entirely new list composed of any split strings. Try printing the output so you can see exactly what is returned.

So the question is what do you then do with this new list of strings?

1 Like

Oh! Sorry
So I tried to clear out all the unnecessary whitespace using .strip method and then append them to a new list.

for record in medical_records:
record_clean = [ ]
for item in record:

That’s where the result starts to deviate from what I expect. The result separate every character in the string like this :
[[‘M’, ‘a’, ‘r’, ‘i’, ‘n’, ‘a’, ‘’, ‘A’, ‘l’, ‘l’, ‘i’, ‘s’, ‘o’, ‘n’, ‘’, ‘’, ‘’, ‘,’, ‘2’, ‘7’, ‘’, ‘’, ‘’, ‘,’, ‘’, ‘’, ‘’, ‘3’, ‘1’, ‘.’, ‘1’, ‘’, ‘,’, ‘’, ‘’, ‘$’, ‘7’, ‘0’, ‘1’, ‘0’, ‘.’, ‘0’, ‘’, ‘’, ‘’].

Whereas if I modify by combining .split method with append syntax as mentioned in the previous section: medical_records.append(record.split(“,”)). The result became readable subsequently :
[[‘Marina Allison’, ‘27’, ‘31.1’, ‘$7010.0’], [‘Markus Valdez’, ‘30’, ‘22.4’, ‘$4050.0’]

1 Like

If you’re posting code to the forums please see- How do I format code in my posts? as it makes it much easier for others to read. If you iterate through a single string it will treat character as an element, for example-

for letter in "this string":
# prints out each letter on by one

Now contrast with the output of using the .split method-

my_string_split = "this string".split()
Out: ['this', 'string']

All good so far? Can you now see what item is being appended?

I strongly encourage you to use both the docs and liberal use of the print function to find out exactly what happens at each stage of your code. This kind of debugging is exceedingly helpful when you’re just starting out and what’s more, you’ll probably continue using something similar from now onwards.

Loops and functions in particular often cause headaches as it isn’t always clear what goes on in inside them so many times newcomers resort to guesswork. That’s a sure-fire way to spend longer than you need to on a problem whilst also dramatically increasing the chance of bugs in the long term. Get some print statements in there and it’s no longer a black box, you can track the logic flow and the arguments passed as closely as you feel like.

Hello guys, I just wanted to know what I was doing incorrectly in this screenshot, since I have the same code in codecademy’s exercise page and it’s running.

Never mind, I noticed that I hadn’t saved the previous variable. :smile: