How to make a list of the lowest number among each month in .csv without python module?

I am trying to list out the minimal number of ticket sold sorting by the month.

csv_file = open(csvfile,"r")
    
date_and_ticket = []
for data in csv_file:
    data = data.replace('n','').split(',')
    date_and_ticket.append(data[0:2])
            
print(date_and_ticket)

From here, I am able to sort out the result like this:

[['16/3/20', '9'], ['17/3/20', '4'], ['18/4/20', '4'], ['19/1/20', '5'], ['17/6/20', '89'], ['18/6/20', '104'], ['19/6/20', '128'], ['20/1/20', '79']]

However, I would like to sort the data according to their months in chronological order and add the value zero if the month is not in the list.

This is what I hope to do:

[5,0,4,4,0,89,0,0,0,0,0,0]

and here is the small portion of the .csv https://drive.google.com/file/d/1aqMwZcSzbY8WpeyTzXP76Sl46acO23bI/view?usp=sharing

Any advice would be greatly appreciated thank you! 🙂

Answer

One way using dict.setdefault to create monthly values:

l = [['16/3/20', '9'], ['17/3/20', '4'], ['18/4/20', '4'], 
     ['19/1/20', '5'], ['17/6/20', '89'], ['18/6/20', '104'], 
     ['19/6/20', '128'], ['20/1/20', '79']]

res = {}
for d, v in l:
    month = int(d.split("/")[1])
    res.setdefault(month, []).append(int(v))

Output:

{1: [5, 79], 3: [9, 4], 4: [4], 6: [89, 104, 128]}

Then dict.get to make 0 for absent months:

[min(res.get(i, [0])) for i in range(1, 13)]

Output:

[5, 0, 4, 4, 0, 89, 0, 0, 0, 0, 0, 0]