Python – splitting a string element within a nested list and group by date

*This is homework. I am not allowed to use any external libraries etc *

I have a nested list of data like:

patients = [ ['Milos', 'Jones', ['15', '01', '20'], 'male', 'smoker', '210'],
         ['Delia', 'Chan', ['15','03','20'], 'female', 'non-smoker', '170'],
         ['Denise', 'Ross', ['13','02','20'], 'female', 'non-smoker', '150'] ]

I need to be able to group the data by month. I know I need to convert the date [’15’, ’01’, ’20’] into integers so I can loop through and match an integer for the month to each month (for range in(1,13)). I am not sure how I can use int() on a nested list to access the correct index position for the date strings.

I have tried:

month_data = []
for line in patients: `
    for i in range(1,13): 
        if int(line[2][1]) == i:
            month_data.append(line[5])
               

I then need to be able to extract the weight depending on the month and create a nested list like below so I can calculate things like averages for each of the months. I don’t know how to use a for loop in a nested list like above and extract a particular element’s values.

data_grouped_month = [[210, 150,200,143,200,201,129],[150,203,187,199,176,154]]

I would appreciate any help. I also really want to understand so will be grateful if an explanation could be given.

ETA – Yes this is homework. The teacher has told us to use all resources available, including telling us about this website for outside of class help. The homework doesn’t count for much as they are more interested in our ability to work it out using any available resources. I have done many other steps with a file to get to this point, but without this step I can’t complete the many other steps I am meant to be learning. Now the exam is another story, if I don’t actually learn and understand then I will have no hope in the exam which is where most of the marks are from.

Answer

If the structure of your data is always the same it shouldn’t be very complicated. You can loop through your list like this:

patients = [
    ["Milos", "Jones", ["15", "01", "20"], "male", "smoker", "210"],
    ["Delia", "Chan", ["15", "03", "20"], "female", "non-smoker", "170"],
    ["Denise", "Ross", ["13", "02", "20"], "female", "non-smoker", "150"],
]

data_grouped_month = [[] for month in range(12)] #You create your nested list 

for patient in patients : #Loop through each patient of the list
    month = int(patient[2][1]) #You get the month and convert it 
    weight = int(patient[5]) #You get the weight and convert it 
    data_grouped_month[month - 1].append(weight) #You add the month to the right place of tour nested list

print(data_grouped_month)

the result is :

[[210], [150], [170], [], [], [], [], [], [], [], [], []]

But it only works if all the months are in the same year (I mean if you have datas for January 2020 and January 2021 it will be grouped together in the same element of data_grouped_month)