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
For people looking for an answer in python 3(.6) without imports.
x is the list to be split.
n is the length of chunks.
L is the new list.
n = 6
L = [x[i:i + int(n)] for i in range(0, (n - 1) * int(n), int(n))]
#[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25]]
Try this:
from __future__ import division
import math
def chunked(iterable, n):
""" Split iterable into ``n`` iterables of similar size
Examples::
>>> l = [1, 2, 3, 4]
>>> list(chunked(l, 4))
[[1], [2], [3], [4]]
>>> l = [1, 2, 3]
>>> list(chunked(l, 4))
[[1], [2], [3], []]
>>> l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(chunked(l, 4))
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
"""
chunksize = int(math.ceil(len(iterable) / n))
return (iterable[i * chunksize:i * chunksize + chunksize]
for i in range(n))
It returns an iterator instead of a list for efficiency (I'm assuming you want to loop over the chunks), but you can replace that with a list comprehension if you want. When the number of items is not divisible by number of chunks, the last chunk is smaller than the others.
EDIT: Fixed second example to show that it doesn't handle one edge case
My answer is to simply use python built-in Slice:
# Assume x is our list which we wish to slice
x = range(1, 26)
# Assume we want to slice it to 6 equal chunks
result = []
for i in range(0, len(x), 6):
slice_item = slice(i, i + 6, 1)
result.append(x[slice_item])
# Result would be equal to
[[0,1,2,3,4,5], [6,7,8,9,10,11], [12,13,14,15,16,17],[18,19,20,21,22,23], [24, 25]]
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]]
If order doesn't matter:
def chunker_list(seq, size):
return (seq[i::size] for i in range(size))
print(list(chunker_list([1, 2, 3, 4, 5], 2)))
>>> [[1, 3, 5], [2, 4]]
print(list(chunker_list([1, 2, 3, 4, 5], 3)))
>>> [[1, 4], [2, 5], [3]]
print(list(chunker_list([1, 2, 3, 4, 5], 4)))
>>> [[1, 5], [2], [3], [4]]
print(list(chunker_list([1, 2, 3, 4, 5], 5)))
>>> [[1], [2], [3], [4], [5]]
print(list(chunker_list([1, 2, 3, 4, 5], 6)))
>>> [[1], [2], [3], [4], [5], []]
more_itertools.divide is one approach to solve this problem:
import more_itertools as mit
iterable = range(1, 26)
[list(c) for c in mit.divide(6, iterable)]
Output
[[ 1, 2, 3, 4, 5], # remaining item
[ 6, 7, 8, 9],
[10, 11, 12, 13],
[14, 15, 16, 17],
[18, 19, 20, 21],
[22, 23, 24, 25]]
As shown, if the iterable is not evenly divisible, the remaining items are distributed from the first to the last chunk.
See more about the more_itertools
library here.