CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-more-itertools

Additional building blocks, recipes, and routines for working with Python iterables beyond itertools.

Pending
Overview
Eval results
Files

iteration-utilities.mddocs/

Iteration Utilities

Core iteration utilities and helpers for advanced iteration patterns.

Capabilities

Functional Iteration

Functions for functional-style iteration patterns.

def iterate(func: Callable[[Any], Any], value: Any) -> Iterator[Any]: ...
def tabulate(func: Callable[[int], Any], start: int = 0) -> Iterator[Any]: ...
def repeatfunc(func: Callable[..., Any], times: int = None, *args: Any) -> Iterator[Any]: ...

Usage:

from more_itertools import iterate, tabulate, repeatfunc
import random

# Generate sequence by repeatedly applying function
fibonacci_like = iterate(lambda x: x[-1] + x[-2], [0, 1])
# Generates: [0, 1], [1, 1], [1, 2], [2, 3], [3, 5], [5, 8], ...

# Tabulate function results
squares = tabulate(lambda x: x**2, 0)
first_squares = [next(squares) for _ in range(5)]  # [0, 1, 4, 9, 16]

# Repeat function calls
random_numbers = repeatfunc(random.random, 5)
rand_list = list(random_numbers)  # 5 random numbers

Exception Handling Iteration

Functions for iterating with exception handling.

def iter_except(func: Callable[[], Any], exception: type | tuple[type, ...], first: Callable[[], Any] = None) -> Iterator[Any]: ...
def iter_suppress(iterable: Iterable[Any], *exceptions: type) -> Iterator[Any]: ...

Usage:

from more_itertools import iter_except, iter_suppress

# Iterate until exception (useful for reading files/queues)
from collections import deque
d = deque([1, 2, 3])

# Read until deque is empty (raises IndexError when empty)
values = list(iter_except(d.popleft, IndexError))  # [1, 2, 3]

# Suppress specific exceptions during iteration
def risky_func(x):
    if x == 2:
        raise ValueError("Skip this")
    return x * 2

data = [1, 2, 3, 4]
safe_iter = iter_suppress((risky_func(x) for x in data), ValueError)
result = list(safe_iter)  # [2, 6, 8] (skips the error case)

Loop Control

Functions for controlling loop behavior.

def loops(n: int) -> Iterator[int]: ...

Usage:

from more_itertools import loops

# Generate exactly n iterations (0 to n-1)
for i in loops(5):
    print(i)  # Prints: 0, 1, 2, 3, 4

Callback and Context Iteration

Functions for iteration with callbacks and context management.

def callback_iter(func: Callable[[], Any], callback_func: Callable[[Any], Any], ignore: type | tuple[type, ...] = ()) -> Iterator[Any]: ...
def with_iter(context_manager: Any) -> Iterator[Any]: ...

Usage:

from more_itertools import callback_iter, with_iter

# Iterate with callback function
def data_source():
    # Simulate data source that eventually exhausts
    import random
    if random.random() < 0.3:
        raise StopIteration
    return random.randint(1, 100)

def log_value(value):
    print(f"Got value: {value}")

# Read data and log each value
values = callback_iter(data_source, log_value, StopIteration)
result = list(values)

# Context manager iteration
import tempfile

# Iterate over context manager
with_temp = with_iter(tempfile.NamedTemporaryFile(mode='w'))
for temp_file in with_temp:
    temp_file.write("Hello, world!")
    break  # Usually you'd have some condition to break

Install with Tessl CLI

npx tessl i tessl/pypi-more-itertools

docs

advanced-utilities.md

combinatorics.md

combining.md

comparison.md

grouping.md

index.md

indexing.md

iteration-utilities.md

lookahead.md

mathematical.md

random-operations.md

selecting.md

sequence-utilities.md

special-purpose.md

summarizing.md

uniqueness.md

utility-classes.md

windowing.md

tile.json