Created
March 29, 2012 17:57
-
-
Save signed0/2240930 to your computer and use it in GitHub Desktop.
Splits a list of items into chunks of length n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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