Additional building blocks, recipes, and routines for working with Python iterables beyond itertools.
—
Functions for dividing iterables into chunks, groups, and partitions.
Break iterables into fixed-size groups.
def chunked(iterable, n, strict=False):
"""
Break iterable into lists of length n.
Args:
iterable: Input iterable to chunk
n: Size of each chunk
strict: If True, raise ValueError if iterable length not divisible by n
Returns:
Iterator of lists, each of length n (except possibly the last)
"""
def ichunked(iterable, n):
"""
Break iterable into iterators of length n.
Args:
iterable: Input iterable to chunk
n: Size of each chunk
Returns:
Iterator of iterators, each yielding up to n items
"""
def batched(iterable, n, *, strict=False):
"""
Group items into batches of size n.
Args:
iterable: Input iterable to batch
n: Batch size
strict: If True, raise ValueError if final batch is incomplete
Returns:
Iterator of tuples, each of length n (except possibly the last)
"""Usage Examples:
from more_itertools import chunked, ichunked, batched
# Basic chunking
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
chunks = list(chunked(data, 3))
# Result: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Iterator chunking (memory efficient)
for chunk in ichunked(range(1000000), 1000):
process_chunk(list(chunk))
# Batched processing
batches = list(batched('ABCDEFG', 3))
# Result: [('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]More sophisticated chunking with constraints and even distribution.
def chunked_even(iterable, n):
"""
Break iterable into n evenly-sized chunks.
Args:
iterable: Input iterable to chunk
n: Number of chunks to create
Returns:
Iterator of n iterators with sizes differing by at most 1
"""
def constrained_batches(iterable, max_size, max_count=None, strict=True):
"""
Create batches with size and count constraints.
Args:
iterable: Input iterable to batch
max_size: Maximum size of each batch
max_count: Maximum number of batches (optional)
strict: If True, raise ValueError on constraint violation
Returns:
Iterator of lists, each satisfying constraints
"""
def sliced(sequence, n, strict=False):
"""
Slice sequence into n-length subsequences.
Args:
sequence: Input sequence to slice
n: Length of each slice
strict: If True, raise ValueError if length not divisible by n
Returns:
Iterator of sequence slices
"""Distribute items into buckets or partition by criteria.
def distribute(children, iterable):
"""
Distribute items from iterable among children.
Args:
children: Number of child iterators to create
iterable: Input iterable to distribute
Returns:
Tuple of children iterators
"""
def divide(n, iterable):
"""
Divide iterable into n approximately equal parts.
Args:
n: Number of parts to create
iterable: Input iterable to divide
Returns:
Iterator of n iterators
"""
def partition(pred, iterable):
"""
Partition iterable based on predicate.
Args:
pred: Predicate function or None
iterable: Input iterable to partition
Returns:
Tuple of (false_items, true_items) iterators
"""
def grouper(iterable, n, incomplete='fill', fillvalue=None):
"""
Group iterable into fixed-length chunks.
Args:
iterable: Input iterable to group
n: Size of each group
incomplete: How to handle incomplete groups ('fill', 'strict', 'ignore')
fillvalue: Value to use for padding when incomplete='fill'
Returns:
Iterator of n-tuples
"""Split iterables at specific points or conditions.
def split_at(iterable, pred, maxsplit=-1, keep_separator=False):
"""
Split iterable at items matching predicate.
Args:
iterable: Input iterable to split
pred: Predicate function to identify split points
maxsplit: Maximum number of splits (-1 for unlimited)
keep_separator: Whether to keep separator items
Returns:
Iterator of sub-iterables
"""
def split_before(iterable, pred, maxsplit=-1):
"""
Split iterable before items matching predicate.
Args:
iterable: Input iterable to split
pred: Predicate function
maxsplit: Maximum number of splits
Returns:
Iterator of sub-iterables
"""
def split_after(iterable, pred, maxsplit=-1):
"""
Split iterable after items matching predicate.
Args:
iterable: Input iterable to split
pred: Predicate function
maxsplit: Maximum number of splits
Returns:
Iterator of sub-iterables
"""
def split_into(iterable, sizes):
"""
Split iterable into groups of specified sizes.
Args:
iterable: Input iterable to split
sizes: Iterable of group sizes
Returns:
Iterator of lists with specified sizes
"""
def split_when(iterable, pred, maxsplit=-1):
"""
Split iterable when predicate becomes true.
Args:
iterable: Input iterable to split
pred: Binary predicate function (prev_item, current_item)
maxsplit: Maximum number of splits
Returns:
Iterator of sub-iterables
"""Group items by key functions or criteria.
def bucket(iterable, key=None, validator=None):
"""
Group items into buckets by key function.
Args:
iterable: Input iterable to bucket
key: Key function (identity if None)
validator: Function to validate keys
Returns:
Bucket object with keyed access to groups
"""
def consecutive_groups(iterable, key=None):
"""
Group consecutive items that differ by 1.
Args:
iterable: Input iterable of numbers
key: Key function to extract numeric values
Returns:
Iterator of iterators, each containing consecutive items
"""Functions that restructure data organization.
def unzip(iterable):
"""
Reverse zip operation - split sequence of tuples.
Args:
iterable: Iterable of tuples/sequences
Returns:
Tuple of iterators, one for each position
"""
def transpose(iterable):
"""
Transpose matrix-like iterable.
Args:
iterable: Iterable of iterables (matrix-like)
Returns:
Iterator of tuples (transposed rows)
"""Install with Tessl CLI
npx tessl i tessl/pypi-more-itertools