Split iterables into evenly sized chunks
pip install philiprehberger-list-chunkSplit iterables into evenly sized chunks.
pip install philiprehberger-list-chunk
from philiprehberger_list_chunk import chunk, chunk_by, sliding_window, interleave, flatten
chunk([1, 2, 3, 4, 5], size=2)
# [[1, 2], [3, 4], [5]]
chunk([1, 2, 3], size=2, pad=0)
# [[1, 2], [3, 0]]
chunk_by([1, 1, 2, 2, 3], key=lambda x: x)
# [[1, 1], [2, 2], [3]]
sliding_window([1, 2, 3, 4, 5], size=3)
# [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
interleave([1, 2, 3], ["a", "b", "c"])
# [1, "a", 2, "b", 3, "c"]
flatten([[1, 2], [3, 4]])
# [1, 2, 3, 4]
Split an iterable into (truthy, falsy) lists in one pass.
from philiprehberger_list_chunk import partition
evens, odds = partition(range(6), lambda n: n % 2 == 0)
# evens == [0, 2, 4], odds == [1, 3, 5]
| Function / Class | Description |
|---|---|
chunk(items, size, pad=None) | Fixed-size chunks |
chunk_by(items, key) | Group consecutive elements by key |
sliding_window(items, size, step=1) | Sliding window views |
interleave(*iterables) | Round-robin interleave |
flatten(nested) | Flatten one level of nesting |
partition(items, predicate) | Split into (truthy, falsy) lists in one pass |
pip install -e .
python -m pytest tests/ -v
If you find this project useful: