What is the best way to divide a list into roughly equal parts? For example, if the list has 7 elements and is split it into 2 parts, we want to get 3 elements in o
If you divide n
elements into roughly k
chunks you can make n % k
chunks 1 element bigger than the other chunks to distribute the extra elements.
The following code will give you the length for the chunks:
[(n // k) + (1 if i < (n % k) else 0) for i in range(k)]
Example: n=11, k=3
results in [4, 4, 3]
You can then easily calculate the start indizes for the chunks:
[i * (n // k) + min(i, n % k) for i in range(k)]
Example: n=11, k=3
results in [0, 4, 8]
Using the i+1
th chunk as the boundary we get that the i
th chunk of list l
with len n
is
l[i * (n // k) + min(i, n % k):(i+1) * (n // k) + min(i+1, n % k)]
As a final step create a list from all the chunks using list comprehension:
[l[i * (n // k) + min(i, n % k):(i+1) * (n // k) + min(i+1, n % k)] for i in range(k)]
Example: n=11, k=3, l=range(n)
results in [range(0, 4), range(4, 8), range(8, 11)]
I tried most part of solutions, but they didn't work for my case, so I make a new function that work for most of cases and for any type of array:
import math
def chunkIt(seq, num):
seqLen = len(seq)
total_chunks = math.ceil(seqLen / num)
items_per_chunk = num
out = []
last = 0
while last < seqLen:
out.append(seq[last:(last + items_per_chunk)])
last += items_per_chunk
return out
#!/usr/bin/python
first_names = ['Steve', 'Jane', 'Sara', 'Mary','Jack','Bob', 'Bily', 'Boni', 'Chris','Sori', 'Will', 'Won','Li']
def chunks(l, n):
for i in range(0, len(l), n):
# Create an index range for l of n items:
yield l[i:i+n]
result = list(chunks(first_names, 5))
print result
Picked from this link, and this was what helped me. I had a pre-defined list.
You can write it fairly simply as a list generator:
def split(a, n):
k, m = divmod(len(a), n)
return (a[i * k + min(i, m):(i + 1) * k + min(i + 1, m)] for i in range(n))
Example:
>>> list(split(range(11), 3))
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10]]
Using list comprehension:
def divide_list_to_chunks(list_, n):
return [list_[start::n] for start in range(n)]
def chunk_array(array : List, n: int) -> List[List]:
chunk_size = len(array) // n
chunks = []
i = 0
while i < len(array):
# if less than chunk_size left add the remainder to last element
if len(array) - (i + chunk_size + 1) < 0:
chunks[-1].append(*array[i:i + chunk_size])
break
else:
chunks.append(array[i:i + chunk_size])
i += chunk_size
return chunks
here's my version (inspired from Max's)