Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created May 25, 2020 22:26
Show Gist options
  • Save hsleonis/4f156fdd87838dc974f25a72003c74e7 to your computer and use it in GitHub Desktop.
Save hsleonis/4f156fdd87838dc974f25a72003c74e7 to your computer and use it in GitHub Desktop.
Chunks a list into smaller lists of a specified size.
from math import ceil

def batch(lst, size):
  return list(
    map(lambda x: lst[x * size:x * size + size],
      list(range(0, ceil(len(lst) / size)))))
batch([1, 2, 3, 4, 5], 2) # [[1,2],[3,4],5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment