How to split a list into n parts in Python

How to split a list into n parts in Python

December 7, 2023(December 8, 2023)
list, python

The split_list_into_n_chunks function in Python allows for dividing a given list into n equally sized sublists. If the list’s length is not perfectly divisible by n, it adjusts some sublist sizes slightly to achieve as even a division as possible.

Function Definition #

def split_list_into_n_chunks(original_list: list, split_num: int) -> list:
    chunk_size, remainder = divmod(len(original_list), split_num)

    chunks = []
    start = 0
    for _ in range(split_num):
        end = start + chunk_size + (1 if remainder > 0 else 0)
        chunks.append(original_list[start:end])
        start = end
        remainder -= 1
    return chunks
from itertools import islice

def split_list_into_n_chunks_iterator(original_list: list, split_num: int) -> list:
    it = iter(original_list)
    return [list(islice(it, chunk_size)) for chunk_size in distribute(len(original_list), split_num)]

def distribute(total_length, num_chunks):
    base_chunk_size, extra = divmod(total_length, num_chunks)
    return [base_chunk_size + 1 if i < extra else base_chunk_size for i in range(num_chunks)]

Differences Between the Two Functions #

The original function directly iterates over the list, creating sublists based on calculated start and end indices.
The iterator version uses Python’s iterator and islice functionalities to achieve the same task.
The iterator version is more memory efficient for large lists, as it processes elements one at a time rather than loading the entire list into memory.

Usage #

To utilize the original function, provide the list to be split and the number of divisions n as arguments.

split_list_into_n_chunks(list, number_of_splits)

For the iterator version of the function, use the following format.

split_list_into_n_chunks_iterator(list, number_of_splits)

Example #

For instance, to divide the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] into 3 parts, use the following code.

test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = 3
split_list_into_n_chunks(test_list, n)

This will result in [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]].

Using the iterator-based function:

split_list_into_n_chunks_iterator(test_list, n)

This will also result in [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]].