Q:

Python: Iterating over a list and inserting data into specific location in another list

I currently have a script that runs an SQL query in a loop that iterates over three terminals and three months for quarterly reports. After each loop, the script inserts the resulting SQL data into one of three lists based on the terminal, and within the terminal, one of three lists for the month.
terminals = [terminal_1, terminal_2, terminal_3]
months = [month_1, month_2, month_3] #ex. 1, 2, 3 for Jan, Feb, Mar
terminal_1Totals = [[]]*3 #three nested lists for months
terminal_2Totals = [[]]*3 
terminal_3Totals = [[]]*3

for t in terminals:
    for m in months:
        #do the query
        #add results to terminal_tTotals for month m
        #close current connection

After the above runs, I'm left with three nested lists with three months worth of data. Each of these 'month' lists contains the following data:
'terminalID', 'transactionNumber', 'year', 'month', 'date (YYMMDD)', 'productCode', 'transaction total'

So far, so good...
What I've done from here, is created a list for each terminal, and within that list, three more lists who's lengths are determined by monthrange(year, monthN):
terminal1_sums = [[0]*monthrange(year, month_1)[1], [0]*monthrange(year, month_2)[1], [0]*monthrange(year, month_3)[1]]
terminal2_sums = [[0]*monthrange(year, month_1)[1], [0]*monthrange(year, month_2)[1], [0]*monthrange(year, month_3)[1]]
terminal3_sums = [[0]*monthrange(year, month_1)[1], [0]*monthrange(year, month_2)[1], [0]*monthrange(year, month_3)[1]]

What I need to do from here is iterate over the 'terminal_NTotals' lists for all transactions with product code '12345', and based on the 'date', column insert (+=) the transaction code to the corresponding position within the 'terminalN_sums' lists. Each month has upwards of 4000 entries, and multiple transactions per day, so I need to sum all the transaction totals for a particular day and insert that total into the list of sums
How can I iterate over the 'Totals' lists, and insert the necessary data into specific locations in terminalN_sum.

A:

If I get what you need, as said Hugo Corrà it's bettere to use classes to make a data structure easy to manage.
Or maybe you can use dicts in a structure like this:
terminals = {'terminal_1': {'month_1': [list of data], 'month_2': [list of data]} etc..}
terminals_sums = {'terminal_1': {'month_1': [sums], 'month_2': [sums]} etc..}

With that you can use terminals['terminal_1']['month_2'][:] to select the data with an index you can use in the terminals_sums dict.

