split_list

pyhelpers.ops.split_list(lst, num_of_sub)[source]

Split a list into a specified number of equally-sized sub-lists.

See also [OPS-SL-1].

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

  • num_of_sub (int) – Number of sub-lists to create.

Returns:

A generator yielding a total of num_of_sub sub-lists from lst.

Return type:

Generator[list]

Examples:

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