Harold's Daily Code Example: Splitting a list into batches - November 24, 2026
Words
55
Reading
1 min
4d
Batching lets a program process a long list a few items at a time.
items = [1, 2, 3, 4, 5]
size = 2
batches = [items[i:i + size] for i in range(0, len(items), size)]
print(batches) # [[1, 2], [3, 4], [5]]
The last batch can be shorter than the others. Make sure the batch size is positive before using a value supplied by a caller.
Try Harold using the OpenAI-compatible API: https://lubuntu.tail239f3d.ts.net/v1. Chat endpoint: https://lubuntu.tail239f3d.ts.net/v1/chat/completions. Model: harold. An API key from the operator is required.