python,python-2.7 , Is there a way to say for every x values, do this?

## Question:

Tag: python,python-2.7

Before I start, I am new to Python, so any low-level description would be incredibly helpful!

I have a list of lets say 60 values (representing one hour, from 8:00-9:00) and I want to run average, maximums, minimums, and standard deviation for each set of 15. (I already have the average, max, mins, and STDEV functions). Is there an understandable way to right this that can be easily scaled to 1,000.000+ values?

The way I am currently doing this does not work and is incredibly inefficient, but I have it posted below:

``````def for_15(tank_data):
for tank in tank_data:
listfifteen = []
tank_dict = []
count = 0
if count <= 14:
count = count + 1
listfifteen.append(tank_data[tank])
tank_dict['0-14 Avg'] = avg(tank_data[tank])
tank_dict['0-14 Max'] = max(tank_data[tank])
tank_dict['0-14 Min'] = min(tank_data[tank])
tank_dict['0-14 STDEV'] = stdev(tank_data[tank])
listfifteen[tank] = tank_dict
print listfifteen
elif count <= 29:
count = count + 1
listfifteen.append(tank_data[tank])
tank_dict['15-29 Avg'] = avg(tank_data[tank])
tank_dict['15-29 Max'] = max(tank_data[tank])
tank_dict['15-29 Min'] = min(tank_data[tank])
tank_dict['15-29 STDEV'] = stdev(tank_data[tank])
listfifteen[tank] = tank_dict
print listfifteen
elif count <= 44:
count = count + 1
listfifteen.append(tank_data[tank])
tank_dict['30-44 Avg'] = avg(tank_data[tank])
tank_dict['30-44 Max'] = max(tank_data[tank])
tank_dict['30-44 Min'] = min(tank_data[tank])
tank_dict['30-44 STDEV'] = stdev(tank_data[tank])
listfifteen[tank] = tank_dict
print listfifteen
else:
count = count + 1
listfifteen.append(tank_data[tank])
tank_dict['45-59 Avg'] = avg(tank_data[tank])
tank_dict['45-59 Max'] = max(tank_data[tank])
tank_dict['45-59 Min'] = min(tank_data[tank])
tank_dict['45-59 STDEV'] = stdev(tank_data[tank])
listfifteen[tank] = tank_dict
print listfifteen
``````

Thanks!

If you do `lst[<start>:<end>]` it would give the elements from `<start>` index (not value) inclusive, to `<end>` (end index) exclusive.

An example might be able to explain this better -

``````>>> lst = [1,2,3,4,5,6,7,8,9,10]
>>> lst[0:5]
[1, 2, 3, 4, 5]
>>> lst[5:10]
[6, 7, 8, 9, 10]
``````

Additionally, if you leave, the first `<start>` part empty, it will by default start from `0` , example -

``````>>> lst[:6]
[1, 2, 3, 4, 5, 6]
``````

Also, if you leave the last `<end>` part empty, it will be default go till the last element ( `len(list)` ) , an example for that -

``````>>> lst[4:]
[5, 6, 7, 8, 9, 10]
``````

Please note , the `[<start>:<end>]` notation produces copy of the list, not the orignal list, so if you do any changes to this copy (unless the list contains references and you do changes inside the referenced object) , the changes would not reflect in original list.

Example -

``````>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> newlst = lst[4:]
>>> newlst
[5, 6, 7, 8, 9, 10]
>>> newlst[0] = 1
>>> newlst
[1, 6, 7, 8, 9, 10]
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
``````