Hi All,
I’d like to propose adding a general-purpose, composable chunking utility to the Python standard library (possibly under itertools
). The goal is to cover a wide range of real-world chunking needs like fixed-size grouping, sliding windows, conditional filtering, and chunk selection logic.
Project Info:
- GitHub: GitHub - catchmaurya/smartchunks: Advanced Python chunking with stride, filtering, and expressions
- PyPI: smartchunks · PyPI
Features:
- Fixed-size chunking
- Sliding window (via
stride
) nth_position
andchunk_position
for advanced selection- Optional padding for incomplete chunks
- Filter function to keep only qualifying chunks
- Materialize as generator or list
Example usage:
python
CopyEdit
from smartchunks import chunked
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Basic chunking
print(list(chunked(data, size=3)))
# → [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Sliding window
print(list(chunked(data, size=3, stride=1)))
# → [[1,2,3], [2,3,4], ..., [7,8,9]]
# Advanced selection
print(list(chunked(data, size=2, nth_position=2)))
# → [[1, 3], [5, 7], [9]]
Would love to hear your feedback on whether this could belong in the standard library or potentially as an enhancement to itertools
.
Thanks so much!
– Maurya Allimuthu