I know this question has been covered many times but my requirement is different.
I have a list like: range(1, 26). I want to divide this list into a fi
This function will return the list of lists with the set maximum amount of values in one list (chunk).
def chuncker(list_to_split, chunk_size):
list_of_chunks =[]
start_chunk = 0
end_chunk = start_chunk+chunk_size
while end_chunk <= len(list_to_split)+chunk_size:
chunk_ls = list_to_split[start_chunk: end_chunk]
list_of_chunks.append(chunk_ls)
start_chunk = start_chunk +chunk_size
end_chunk = end_chunk+chunk_size
return list_of_chunks
Example:
ls = list(range(20))
chuncker(list_to_split = ls, chunk_size = 6)
output:
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19]]