Problem 129505 · medium · Level 01 Prerequisites & Setup

Split Into Rows

lists · nested-lists · slicing

A photo gallery shows size pictures per row. Write split_rows(items, size) that returns a new list of rows, where each row is a list of size consecutive items from items. The last row may be shorter. Return an empty list for an empty input.

Examples

Input:  items = [1, 2, 3, 4, 5], size = 2
Output: [[1, 2], [3, 4], [5]]

Input:  items = [], size = 3
Output: []

Constraints

  • 0 <= len(items) <= 5000
  • 1 <= size <= 5000

Goals

  • Build a list of lists from a flat list
  • Use a range with a step to jump by chunk size
Starting Python…