Skip to content

Instantly share code, notes, and snippets.

@rlcarrca
Created September 17, 2025 01:30
Show Gist options
  • Save rlcarrca/e18ec1acce08b3b4155ed9d80349a68f to your computer and use it in GitHub Desktop.
Save rlcarrca/e18ec1acce08b3b4155ed9d80349a68f to your computer and use it in GitHub Desktop.
Chunk List
def batch(seq, size):
return [
seq[i:i + size]
for i in range(0, len(seq), size)
]
seq = [1,2,3,4,5,6]
size = 3
# OUTPUT: [[1,2,3],[4,5,6]]
print(batch(seq, size))
seq = [1,2,3,4,5]
size = 2
# OUTPUT: [[1,2],[3,4],[5]]
print(batch(seq, size))
seq = [1,2,3]
size = 4
# OUTPUT: [[1,2,3]]
print(batch(seq, size))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment