Skip to content

Instantly share code, notes, and snippets.

@neara
Last active October 3, 2019 19:27
Show Gist options
  • Save neara/955eb5cc6648c78518bc5bd6c51a13c6 to your computer and use it in GitHub Desktop.
Save neara/955eb5cc6648c78518bc5bd6c51a13c6 to your computer and use it in GitHub Desktop.
Small utilities that work in python 2.7 and python 3.7+
def batchify(items_list, batch_size=500):
"""Generate batches from items list.
Args:
items_list (list): list that will be batchified.
batch_size (int, default=500): batch size that will be returned.
last batch is not promised to be exactly the length of batch_size.
Returns:
subset of items_list
"""
total = len(items_list)
left_to_process = total
start = 0
while left_to_process >= 0:
end = start + batch_size
if end > total:
end = total
yield items_list[start:end]
start += batch_size
left_to_process -= batch_size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment