Split requests into batches

lists

Split requests into batches

Stripe Python Interview Question

Stripe's API accepts at most a fixed number of records per request, so a long list of records must be sent in batches.

Write a function chunk(items, size) that splits the list into consecutive batches of the given size and returns a list of those batches. The last batch can be smaller.

Asked of

  • Data Analyst
  • Data Engineer
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

items = [1, 2, 3, 4, 5, 6, 7], size = 3

Output

[[1, 2, 3], [4, 5, 6], [7]]

Example 2

Input

items = ["a", "b"], size = 5

Output

[["a", "b"]]

Explanation

In the first example, 7 records split into batches of 3 give two full batches and a final batch with the single record 7.

Submit also runs 3 hidden test cases that check edge cases.