I have a list of dict
s I need to be divided into chunks:
input_size = len(input_rows) # num of dicts
slice_size = int(input_size / 4) # size of each chunk
remain = input_size % 4 # num of remaining dicts which cannot be divided into chunks
result = [] # initializes the list for containing lists of dicts
iterator = iter(input_rows) # gets a iterator on input
for i in range(4):
result.append([]) # creates an empty list as an element/block in result for containing rows for each core
for j in range(slice_size):
result[i].append(iterator.__next__()) # push in rows into the current list
if remain:
result[i].append(iterator.__next__()) # push in one remainder row into the current list
remain -= 1
input_rows
contains a list of dict
s, divide it into 4 chunks/slices; if there are any remaining dict
s that cannot be evenly divided into 4 chunks, these remaining dict
s will be put into some of the chunks. A list (result
) is used to contain each chunk, which in turn contains a list of dict
s.
I am wondering how to do it in a more efficient way.