Skip to main content

Split iterables into evenly sized chunks.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_list_chunk-0.3.0.tar.gz (174.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_list_chunk-0.3.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_list_chunk-0.3.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_list_chunk-0.3.0.tar.gz
Algorithm Hash digest
SHA256 76610cbf7112c1feda9c0b009f7474bc9280f406fbd28691f03624608b19f32b
MD5 54a65bc4ec81aa5d11ef23063ce9e04d
BLAKE2b-256 ad057a9f25b20058e245a43db74c7c09497cf9cf6f64fda4affe2fb8b7bed077

See more details on using hashes here.

File details

Details for the file philiprehberger_list_chunk-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_list_chunk-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 642680914484d2c6758208beeea8ef7d779e0e802f25ab40abe32f092fee3b00
MD5 5c0178e899e65e7628a9bd289f5ed216
BLAKE2b-256 88d0bde776e1e13d6e7b3134dba648b45495e513ad92f1b54ab5a133e7fe8cc6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page