How to split a List into equally sized chunks in Python
Breaking down a list into equally sized chunks is a common task in Python, essential for efficient data processing. Let's explore various methods to achieve this, each catering to different needs and preferences.
1. Implement Your Own Generator
my_list = list(range(10))
def chunk(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
chunks = list(chunk(my_list, 3))
# Output: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
Creating a simple generator function provides flexibility and readability.
2. One-Liner Marvel
n = 3
# Generator comprehension
chunks = (my_list[i:i + n] for i in range(0, len(my_list), n))
print(list(chunks))
# Output: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
# List comprehension
chunks = [my_list[i:i + n] for i in range(0, len(my_list), n)]
print(chunks)
# Output: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
For those who appreciate concise code, one-liners provide elegance without compromising functionality.
3. itertools.zip_longest Magic
from itertools import zip_longest
def chunk(lst, n):
return zip_longest(*[iter(lst)]*n, fillvalue=None)
chunks = list(chunk(my_list, 3))
# Output: [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]
This method employs the zip_longest
function from the itertools
module, ensuring uniformity even with uneven chunks.
4. itertools.islice Wisdom
from itertools import islice
def chunk(lst, n):
it = iter(lst)
return iter(lambda: tuple(islice(it, n)), ())
chunks = list(chunk(my_list, 3))
# Output: [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)]
itertools.islice
offers a neat solution, leaving the last batch incomplete if it's shorter than the specified size.
5. Embracing Python 3.12: itertools.batched
# Available in Python 3.12
from itertools import batched
chunks = list(batched(my_list, 3))
# Output: [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)]
For those on the cutting edge with Python 3.12, the itertools.batched
method simplifies the process.
6. Leverage more-itertools for Versatility
# Install more-itertools first: pip install more-itertools
import more_itertools as mit
list(mit.chunked(my_list, 3))
# Output: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
The more-itertools
library extends your toolkit, providing additional methods like chunked
, sliced
, grouper
, and more.
By understanding these methods, you can confidently tackle the task of splitting lists into equally sized chunks, choosing the approach that aligns with your code style and requirements.