Working on an assignment, which in one of the parts requires me to split the list into "x" chunks of lists but I don't necessarily want to use the elements inside the list to do so as I have seen many examples that do this. For example, given the list [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
, I want to split this list into 3 even parts ([[1,2,3,4,5],[6,7,8,9,10],[11,12,14,15]])
.
Most of the examples that I have seen on here tend to divide the whole list by "x" rather than making "x" groups of it which end up giving me a list like [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]
.
EDIT:
I've figured out how to get the desired output in the first part of this question that I was looking for but upon looking at my assignment further, I realized the lists that need to be made have to go up in increments of lst
rather than having to put a value in which would generate a list in that range. For example if I want to print out a grouped list like [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
I don't want to have have to set lst
to lst = list(range(1,lst+1))
and then when I'm assigning values to my function, I want to be able to put 5 in place of 15 to get the same result.
def test(num, lst, start = 1):
lst = list(range(start,lst+1))
avg = len(lst) / float(num)
out = []
last = 0.0
while last < len(lst):
out.append(lst[int(last):int(last + avg)])
last += avg
return out
print (test(3,15))