Skip to content

Instantly share code, notes, and snippets.

@signed0
Created March 29, 2012 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save signed0/2240930 to your computer and use it in GitHub Desktop.
Save signed0/2240930 to your computer and use it in GitHub Desktop.
Splits a list of items into chunks of length n
from itertools import islice
def batch(items, n):
'''Splits a list of items into chunks of length n'''
i = iter(items)
while True:
items = tuple(islice(i, n))
if len(items) == 0:
break
yield items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment