# Nested loops (for)

Hello to the whole community. I am new to this language and would like to ask for your help please. I have 8760 electricity production data, that is, the amount of energy for each hour of the year. However, I would like to have only one average day for each month (I put a picture for reference). Could you help me or give me some idea how I could do this please.

Obviously, for each month I have 28, 30 or 31 days, depending on the month.
I have tried to divide the entire list of data, into sublists of each month and then each month in days, to be able to handle it better. Then for each month, I had thought of using something like:
for k in range (0.31):
for j in range (0,24):

Thank you very much in advance

from “one average day for each month” and the picture you attached, I conclude that you want the average electricity consumption in each hour in the respective month.

Also, you did not mention in which format the raw data is. So, I consider it is in the format - list of hours in list of days in list of months.
For example, to access a 13th hour of 2nd day of 4th month, we would use - `val = data[3][1][12]`

(If you don’t have the data in this format, I would suggest you format it in such simple readable formats as a good practice)

``````leap = False
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if leap:
months[1] += 1

output = [12][24]

for m in range(12):
for h in range(24):
sm = 0
for d in range(months[m]):
sm += data[m][d][h]
avg = sm/24
output[m][h] = avg
``````

I intentionally won’t put an explanation, as you should be able to understand it yourself. Still if you find trouble, or have any edits, feel free to reply.

Hi @fmoraga ,
If you don’t want to use nested loops, You can try with `numpy` and a little bit of matrix transformation knowledge.
Here is a sample about calculating Feb’s data:

Thank you very much for your kind help Sanket, but my data is simply in a list of size 8760. I know that each item on the list corresponds to the production of each hour of the year, but I do not have it ordered as you tell me. Could you give me some advice on how to sort my data by month and day tables, please.

In the code that I previously gave, keep all the loops the way are. Just initialise a variable cur_day to 0 at the very beginning. Now, in the innermost loop access your day by using data[cur_day]. Perform the sum operation, and increment cur_day.

So ultimately you don’t have to modify the existing data.

If you still want to organise it the way I said, you can use some moderate knowledge of Pandas.