Split a List into Chunks in Python

To split a list into chunks in Python, you can use list slicing, list comprehensions, or the itertools module. The most common approach is using a loop or list comprehension with slicing. This method allows you to divide a list into smaller sublists of a specified size efficiently.


Examples

1. Splitting a List into Chunks Using List Slicing

One of the simplest ways to split a list into chunks is by using list slicing in a loop.

</>
Copy
# Function to split a list into chunks
def split_into_chunks(lst, chunk_size):
    return [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)]

# Sample list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Splitting the list into chunks of size 3
chunked_list = split_into_chunks(numbers, 3)

# Printing the result
print("Chunks:", chunked_list)

Explanation:

In this example, the function split_into_chunks() takes two arguments:

  1. lst: The input list to be split.
  2. chunk_size: The number of elements in each chunk.

The function uses list comprehension with slicing to create sublists of length chunk_size by iterating through the original list in steps of chunk_size.

Output:

Chunks: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

2. Splitting a List into Chunks Using a Generator

Generators are a memory-efficient way to split a list into chunks.

</>
Copy
# Generator function to yield chunks
def chunk_generator(lst, chunk_size):
    for i in range(0, len(lst), chunk_size):
        yield lst[i:i + chunk_size]

# Sample list
data = [10, 20, 30, 40, 50, 60, 70, 80, 90]

# Using the generator to get chunks of size 4
chunked_data = list(chunk_generator(data, 4))

# Printing the result
print("Chunks:", chunked_data)

Explanation:

The function chunk_generator():

  1. Uses a for loop to iterate through the list with a step of chunk_size.
  2. Uses yield instead of returning a full list, making it memory efficient.
  3. Converts the generator output to a list using list() before printing.

Output:

Chunks: [[10, 20, 30, 40], [50, 60, 70, 80], [90]]

3. Splitting a List Using itertools

The itertools module provides an efficient way to split a list into chunks.

</>
Copy
from itertools import islice

# Function to split list into chunks using itertools
def split_using_islice(lst, chunk_size):
    it = iter(lst)
    return [list(islice(it, chunk_size)) for _ in range(0, len(lst), chunk_size)]

# Sample list
values = [5, 15, 25, 35, 45, 55, 65, 75, 85, 95]

# Splitting into chunks of size 2
chunks = split_using_islice(values, 2)

# Printing the result
print("Chunks:", chunks)

Explanation:

  1. The function split_using_islice() uses iter(lst) to create an iterator.
  2. islice() extracts chunk_size elements at a time from the iterator.
  3. The result is a list of lists containing chunks of the specified size.

Output:

Chunks: [[5, 15], [25, 35], [45, 55], [65, 75], [85, 95]]

Conclusion

There are multiple ways to split a list into chunks in Python:

  1. List Slicing: Uses simple list comprehension for splitting.
  2. Generators: Provides memory-efficient chunking.
  3. itertools.islice(): Efficient for large datasets.

Choose the best method depending on your use case. If you need a simple approach, list slicing is ideal. For large datasets, generators or itertools offer better memory efficiency.