Last modified: Aug 16, 2026
Python Array Split into Chunks Guide
Splitting a large array into smaller chunks is a common task in Python. You might need it for batch processing, parallel tasks, or just to manage memory better. This guide shows you simple and effective ways to do it.
We will cover three main methods. First, we'll use list comprehension for a quick one-liner. Next, we'll build a reusable function with loops. Finally, we'll see how the numpy library makes it even easier. Each method has its own strength, so you can pick what fits your project.
Why Split Arrays into Chunks?
Large datasets can slow down your program. Processing data in smaller pieces helps you avoid memory errors. It also lets you show progress or handle errors without losing everything.
For example, if you're sending data to an API, you might need to send it in parts. Or if you're training a machine learning model, you feed it batches. Splitting arrays is a fundamental skill for these scenarios.
This technique works with lists, tuples, and other iterable objects. We'll focus on arrays and lists since they are most common. The logic remains the same across all data types.
Method 1: Using List Comprehension
The fastest way to split an array is with list comprehension. It's clean, readable, and perfect for simple cases. You define the chunk size and slice the array accordingly.
Here's the basic pattern. We use range() to step through the array by the chunk size. Then we slice from the current index to the next chunk boundary.
# Original array
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Define chunk size
chunk_size = 3
# Split using list comprehension
chunks = [data[i:i + chunk_size] for i in range(0, len(data), chunk_size)]
print(chunks)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
Notice the last chunk has only one element. That's fine. The method naturally handles uneven sizes. This one-liner is efficient and easy to remember.
If you have a very large array, this method still works well. It creates a new list of chunks, so memory usage is proportional to the output. For most tasks, this is perfectly acceptable.
Method 2: Using a Loop Function
For more control, you can write a function with a loop. This is useful when you need to do extra processing on each chunk. It also makes your code reusable across different parts of your program.
Let's create a function called split_into_chunks(). It takes an array and a chunk size as arguments. Inside, we loop and append each slice to a results list.
def split_into_chunks(arr, size):
"""Split an array into chunks of given size."""
chunks = []
for i in range(0, len(arr), size):
chunk = arr[i:i + size]
chunks.append(chunk)
return chunks
# Test the function
my_array = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
result = split_into_chunks(my_array, 2)
print(result)
[['a', 'b'], ['c', 'd'], ['e', 'f'], ['g']]
This function is clear and straightforward. You can easily add error handling. For instance, you might want to raise an error if the chunk size is zero or negative. That makes your code more robust.
Using a function also helps with testing. You can write unit tests for different array sizes and chunk sizes. This ensures your logic works in all edge cases.
If you are working with array modules, this function works too. Just pass the array object, and slicing behaves the same. Check our guide on array module vs list for more details.
Method 3: Using NumPy's array_split
If you're already using numpy, there's an even better way. The numpy.array_split() function handles splitting automatically. It also deals with uneven chunks gracefully.
First, you need to install numpy if you haven't. Then you can convert your list to a numpy array and split it. Here's how it works.
import numpy as np
# Create a numpy array
data = np.array([10, 20, 30, 40, 50, 60, 70])
# Split into 3 chunks (not fixed size)
chunks = np.array_split(data, 3)
# Print each chunk
for chunk in chunks:
print(chunk)
[10 20 30]
[40 50]
[60 70]
Notice that array_split tries to make chunks as equal as possible. The first chunk has 3 elements, the others have 2. This is different from slicing by a fixed size.
Numpy is very efficient for numerical data. If you're doing heavy computation, this is the best choice. It also integrates well with other numpy functions like reshape and concatenate.
For more advanced array operations, check our guide on array merge and sort. It shows how to combine chunks back together if needed.
Handling Edge Cases
What if your array is empty? Or the chunk size is larger than the array? Let's see how each method handles these situations.
List comprehension returns an empty list if the array is empty. If the chunk size is larger, it returns the whole array as one chunk. That's usually what you want.
# Edge case examples
empty = []
print([empty[i:i+3] for i in range(0, len(empty), 3)])
short = [1, 2]
print([short[i:i+5] for i in range(0, len(short), 5)])
[]
[[1, 2]]
The loop method behaves the same way. It's safe to use with any input. Just make sure your chunk size is a positive integer. Otherwise, you might get an infinite loop or an error.
Numpy's array_split raises an error if you ask for zero or negative chunks. It's better to validate your input before calling it. You can use a simple if statement to check.
For more on array type handling, see our type casting guide. It helps you ensure your data is in the right format before splitting.
Performance Considerations
When dealing with huge arrays, performance matters. List comprehension is the fastest pure Python method. It's optimized for speed and memory usage.
Numpy is even faster for large numerical datasets. It uses C-level operations under the hood. If you're processing millions of numbers, numpy is the way to go.
Loops are the slowest, but they offer the most flexibility. Use them when you need to do complex operations on each chunk. The performance difference is negligible for small arrays.
Remember, splitting creates new arrays. This uses extra memory. If memory is a concern, consider processing chunks one at a time instead of storing them all.
You can also use generators to yield chunks. This avoids storing all chunks at once. It's a more advanced technique but very useful for streaming data.
For more on memory management, read our article on array memory allocation. It explains how Python stores arrays and why slicing works the way it does.
Practical Example: Batch Processing
Let's put it all together with a real-world example. Suppose you have a list of numbers and you want to process them in batches of 4. You'll square each number and print the results.
numbers = list(range(1, 13)) # 1 to 12
batch_size = 4
for batch in [numbers[i:i+batch_size] for i in range(0, len(numbers), batch_size)]:
squared = [n ** 2 for n in batch]
print(f"Batch: {batch} -> Squared: {squared}")
Batch: [1, 2, 3, 4] -> Squared: [1, 4, 9, 16]
Batch: [5, 6, 7, 8] -> Squared: [25, 36, 49, 64]
Batch: [9, 10, 11, 12] -> Squared: [81, 100, 121, 144]
This pattern is extremely common in data pipelines. You can adapt it to read files, call APIs, or train models. The key is to keep your code readable and maintainable.
If you need to iterate over chunks without storing them, use a generator. We'll show that next.
Advanced: Using Generators
Generators are memory-efficient. They produce chunks on the fly instead of storing all of them. This is perfect for large files or infinite streams.
Here's how to write a generator function. Instead of return, you use yield. Each time you call next(), it gives you the next chunk.
def chunk_generator(arr, size):
"""Yield chunks of an array one at a time."""
for i in range(0, len(arr), size):
yield arr[i:i + size]
# Use the generator
data = [1, 2, 3, 4, 5, 6]
for chunk in chunk_generator(data, 2):
print(chunk)
[1, 2]
[3, 4]
[5, 6]
Generators don't store all chunks in memory. This makes them ideal for huge datasets. You can process each chunk and discard it immediately.
This approach pairs well with file reading. You can read a large file line by line and group them into chunks. It's a powerful pattern for data engineering.
For more on iterating through arrays, see our iteration guide. It covers different loop styles and when to use them.
Conclusion
Splitting arrays into chunks is a simple yet powerful technique. We've covered three main methods: list comprehension, loop functions, and numpy's array_split. Each has its place depending on your needs.
Start with list comprehension for quick tasks. Move to a loop function when you need more control. Choose numpy for heavy numerical work. And use generators for memory efficiency.
Practice with your own data to get comfortable. Try different chunk sizes and see how the output changes. The more you experiment, the more natural it becomes.
Remember to handle edge cases and validate your inputs. This prevents bugs and makes your code robust. Happy coding!