Skip to main content

philiprehberger-list-chunk

Tests PyPI version Last updated

philiprehberger-list-chunk

Split iterables into evenly sized chunks.

Installation

pip install philiprehberger-list-chunk

Usage

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]

Transpose and pairwise

transpose(matrix) swaps rows and columns and truncates to the shortest row, so jagged input stays rectangular. pairwise(items) returns consecutive overlapping pairs.

from philiprehberger_list_chunk import transpose, pairwise

transpose([[1, 2, 3], [4, 5, 6]])
# [[1, 4], [2, 5], [3, 6]]

pairwise([1, 2, 3, 4])
# [(1, 2), (2, 3), (3, 4)]

Partition

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]

API

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
transpose(matrix) Transpose a 2D iterable; jagged input is truncated to the shortest row
pairwise(items) Return consecutive overlapping (a, b) tuples

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

⭐ Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Release files for philiprehberger-list-chunk 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for philiprehberger-list-chunk 0.3.0
File Size Uploaded
philiprehberger_list_chunk-0.3.0.tar.gz 174.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-list-chunk 0.3.0
File Interpreter ABI Platform
philiprehberger_list_chunk-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 180.2 kB

Release files / philiprehberger_list_chunk-0.3.0.tar.gz

Download URL philiprehberger_list_chunk-0.3.0.tar.gz
Size 174.8 kB
Tags Source
SHA-256 checksum
How to use checksums
76610cbf7112c1feda9c0b009f7474bc9280f406fbd28691f03624608b19f32b
BLAKE2b-256 checksum
How to use checksums
ad057a9f25b20058e245a43db74c7c09497cf9cf6f64fda4affe2fb8b7bed077
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_list_chunk-0.3.0-py3-none-any.whl

Download URL philiprehberger_list_chunk-0.3.0-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
642680914484d2c6758208beeea8ef7d779e0e802f25ab40abe32f092fee3b00
BLAKE2b-256 checksum
How to use checksums
88d0bde776e1e13d6e7b3134dba648b45495e513ad92f1b54ab5a133e7fe8cc6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page