split_list_by_size

pyhelpers.ops.split_list_by_size(lst, sub_len)[source]

Split a list into evenly sized sub-lists.

See also [OPS-SLBS-1].

Parameters:
  • lst (list) – List to be split.

  • sub_len (int) – Length of each sub-list.

Returns:

A generator yielding sub-lists of length sub_len from lst.

Return type:

Generator[list]

Examples:

>>> from pyhelpers.ops import split_list_by_size
>>> lst_ = list(range(0, 10))
>>> lst_
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lists = split_list_by_size(lst_, sub_len=3)
>>> list(lists)
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]