Skip to main content

English description follows Japanese.


リストやその他のイテラブルを指定サイズのバッチに分割するシンプルなPythonユーティリティです。

概要

batch-div はリストをはじめとする任意のイテラブルを、指定サイズの部分リスト(バッチ)に分割します。並列処理やバッチ処理など、データをまとめて処理したい場面で便利です。

itertools.batched(Python 3.12以降)と同様の機能を、古いバージョンのPythonでも利用できます。結果はすべてリストとして返されるため、外側・内側ともに len() が使え、進捗表示に適しています。ただし、すべてリストとしてメモリに展開するため、メモリに載りきらないほど巨大なデータには不向きです。

インストール

pip install batch-div

使い方

import batch_div

tasks = [0, 1, 2, 3, 4, 5, 6]
for batch in batch_div(tasks, 3):
    print(batch)

出力:

[0, 1, 2]
[3, 4, 5]
[6]

第1引数に分割したいイテラブル(リスト、タプル、ジェネレータなど)、第2引数にバッチサイズを指定します。入力は先頭から順にバッチサイズごとに分割され、割り切れない場合は最後のバッチに残りの要素が入ります。

活用例

import batch_div

items = list(range(100))
batches = batch_div(items, 10)
print(f"全バッチ数: {len(batches)}")  # -> 10

for i, batch in enumerate(batches):
    print(f"バッチ {i+1}/{len(batches)} を処理中 ({len(batch)} 件)")
    # ここに並列処理などを記述

ライセンス

CC0 1.0 Universal(パブリックドメイン)


A simple Python utility for splitting a list or any iterable into batches of a specified size.

Overview

batch-div divides a list — or any iterable — into sublists (batches) of a given size. It is useful when you want to process data in chunks — for example, in parallel or batch processing workflows.

Similar to itertools.batched (Python 3.12+), but works on older Python versions. All results are returned as lists, so len() is supported on both the outer and inner collections — handy for progress tracking. Note that because everything is materialized as lists, this tool is not suitable for extremely large datasets that don't fit in memory.

Installation

pip install batch-div

Usage

import batch_div

tasks = [0, 1, 2, 3, 4, 5, 6]
for batch in batch_div(tasks, 3):
    print(batch)

Output:

[0, 1, 2]
[3, 4, 5]
[6]

The first argument is any iterable to split (lists, tuples, generators, etc.), and the second is the batch size. The input is divided into consecutive chunks of that size, with the last batch containing the remaining elements if the input doesn't divide evenly.

Use Case Example

import batch_div

items = list(range(100))
batches = batch_div(items, 10)
print(f"Total batches: {len(batches)}")  # -> 10

for i, batch in enumerate(batches):
    print(f"Processing batch {i+1}/{len(batches)} ({len(batch)} items)")
    # Insert your parallel or batch processing here

License

CC0 1.0 Universal (Public Domain Dedication)

Metadata

Release files for batch-div 0.1.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 batch-div 0.1.0
File Size Uploaded Provenance
batch_div-0.1.0.tar.gz 3.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for batch-div 0.1.0
File Interpreter ABI Platform
batch_div-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.2 kB

Release files / batch_div-0.1.0.tar.gz

Download URL batch_div-0.1.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7265352d05003856f8e9656a5212d1b40e2996ae55b9da3873fc23c14f2938ca
BLAKE2b-256 checksum
How to use checksums
8fa5cdf9e0a8e72416744535127f9650301a897bc01fe538fbe4c1c57c844a12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.3

Release files / batch_div-0.1.0-py3-none-any.whl

Download URL batch_div-0.1.0-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
39b279d3b1c707a328c7f19c379a4b6bfc59a5a29348cce39831de1594ff8758
BLAKE2b-256 checksum
How to use checksums
6cc19c15f945d4d154ff85cc52c2e8572b8e6d37050708b426186cd8fd9ea1da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.3

Security for batch-div 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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