5 Best Ways to Split a Python List into Chunks

πŸ’‘ Problem Formulation: In Python programming, a common task is to take a list and break it down into smaller sublists, or chunks, of a specific size. For instance, you might want to split a list of 12 elements into chunks of 4. The input would be a list such as [1, 2, 3, ..., 12], and the desired output is a nested list where each inner list contains 4 elements, like [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]].

Method 1: Using a For Loop

The for loop method iterates through the input list, generating a new chunk at each step until it reaches the end of the list. It is straightforward, easy to understand, and requires no additional imports.

β™₯️ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month

Here’s an example:

def chunks(lst, chunk_size):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), chunk_size):
        yield lst[i:i + chunk_size]

my_list = [i for i in range(1, 13)]
list_of_chunks = list(chunks(my_list, 4))

Output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

This code defines a function chunks that yields chunks of a given size from the provided list lst. It uses a for loop and the range function to yield slices of the list from the beginning to the end in increments of chunk_size. This method is easy to implement and works with any iterable, not just lists.

Method 2: List Comprehension

List comprehension in Python is a compact way to process all or part of the elements in a sequence and return a list with the results. This method leverages list comprehension for its conciseness and Pythonic style.

Here’s an example:

my_list = [i for i in range(1, 13)]
chunk_size = 4
list_of_chunks = [my_list[i:i + chunk_size] for i in range(0, len(my_list), chunk_size)]

Output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

This snippet uses list comprehension to create chunks from the my_list. Similar to the for loop method, it slices the list in steps of chunk_size, which is a more compact and Pythonic approach.

Method 3: Using the zip Function

The zip function in Python can be used creatively to pair up elements in a sequence and form chunks. This method requires an additional step of “zipping” iterators together but is efficient and fast.

Here’s an example:

my_list = [i for i in range(1, 13)]
chunk_size = 4
args = [iter(my_list)] * chunk_size
list_of_chunks = list(zip(*args))

Output:

[(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)]

This code creates an iterator from the list and repeats it chunk_size times. When passed to the zip function, it combines these iterators into tuples of length chunk_size. Note that this method may drop elements if the list length is not a multiple of chunk_size.

Method 4: Using itertools.islice()

The islice() function from the itertools module can slice an iterator. This method is handy when dealing with large datasets as it doesn’t require the input list to be loaded into memory all at once.

Here’s an example:

from itertools import islice

def chunks(lst, chunk_size):
    it = iter(lst)
    return [list(islice(it, chunk_size)) for _ in iter(lambda: True, False)]

my_list = [i for i in range(1, 13)]
list_of_chunks = chunks(my_list, 4)

Output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

The chunks function creates an iterator from the list and uses a list comprehension with islice() to form sublists of chunk_size. It’s efficient for large lists because it slices without copying.

Bonus One-Liner Method 5: Using numpy.array_split()

If you are working within the scientific computing ecosystem and have numpy available, numpy.array_split() is a powerful one-liner that handles splitting an array efficiently. This method is especially efficient for numeric data and depends on the numpy library.

Here’s an example:

import numpy as np

my_list = [i for i in range(1, 13)]
list_of_chunks = np.array_split(np.array(my_list), 3)

Output:

[array([1, 2, 3, 4]), array([5, 6, 7, 8]), array([9, 10, 11, 12])]

This code converts the list into a numpy array and uses array_split() to divide it into 3 chunks. This method provides the additional benefits of numpy’s array processing capabilities.

Summary/Discussion

  • Method 1: For Loop. Simple and easy to implement. Does not require any external libraries. Not the most Pythonic method.
  • Method 2: List Comprehension. Compact and readable. Pythonic and efficient for small to medium lists. May use more memory for large lists.
  • Method 3: zip Function. Clever use of iterators. Efficient and fast, but can drop elements if the list length is not divisible by chunk_size.
  • Method 4: itertools.islice(). Great for large datasets, as it does not require loading the entire list into memory. More code compared to other methods.
  • Method 5: numpy.array_split(). Best for numeric data and within numpy’s ecosystem. Depends on an external package, which may not be suitable for all environments.