Loading video player…

Splitting Iterables Into Batches

00:00 In this lesson, you’ll fix the lazy grouper for data that doesn’t divide evenly, first with a classic itertools recipe, and then with a function that does the whole job for you.

00:11 Ask better_grouper() for groups of 4, and something goes missing. You get two groups, and 9 and 10 have vanished. zip() stops as soon as its shortest input runs out.

00:22 To fix that, import itertools. You’ll use the short name it from now on. Take a list of five numbers and a list of three letters.

00:33 zip() gives you three pairs and drops the rest. zip_longest() keeps going until the longest input runs out, and it fills the gaps with None.

00:43 Swap zip() for zip_longest() in the lazy grouper, and you get the grouper() recipe. It’s the same shared iterator trick plus a fill value for the last group.

00:57 Now 9 and 10 survive, padded with None. A version of this recipe lives in the itertools docs, and the recipe section there is a great source of ideas.

01:09 Here’s the difference between the two approaches. The grouper() recipe always hands you full groups, so the last one gets padded with None.

01:17 That’s useful sometimes, but often you have to strip the padding back out. Since Python 3.12, itertools has a function called batched().

01:24 It returns the same full groups, and the last batch is just shorter.

01:30 Now try out batched() in the REPL. Batches of 4 give you two full batches and a short one with 9 and 10. No padding needed. If a short batch would be a bug in your program, then pass strict=True, which arrived in Python 3.13.

01:48 Now batched() raises a ValueError. With batches of 5, everything fits, so strict mode is happy.

01:57 In this lesson, you met zip_longest() and the grouper() recipe, and then batched(), the built-in way to split any iterable into chunks.

02:05 Like the recipe, batched() never calls len(), so it works on infinite iterators too. You’ll come back to batched() at the end of the course to pick relay teams.

02:15 Next up, though, it’s time to brute-force a classic interview puzzle.

Become a Member to join the conversation.