How to Split a List into Equal Sized Chunks in Python?

The following code shows how to split a list into equal sized chunks in Python:


def to_chunks(list, chunk_size):
    final_list = []
    for i in range(0, len(list), chunk_size):
        final_list.append(list[i:i+chunk_size])
    return final_list

my_list = [3, 4, 9, 7, 1, 1, 2, 3]
print(to_chunks(my_list, 3))
Output:
[[3, 4, 9], [7, 1, 1], [2, 3]]