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

combining.mddocs/

Combining and Augmenting

Functions for merging, combining, and augmenting iterables.

Capabilities

Interleaving Operations

Combine multiple iterables in various patterns.

def interleave(*iterables):
    """
    Round-robin interleaving of multiple iterables.
    
    Args:
        *iterables: Variable number of iterables to interleave
        
    Returns:
        Iterator yielding items in round-robin fashion
    """

def interleave_longest(*iterables):
    """
    Interleave until the longest iterable is exhausted.
    
    Args:
        *iterables: Variable number of iterables to interleave
        
    Returns:
        Iterator yielding items until longest is exhausted
    """

def interleave_evenly(iterables, lengths=None):
    """
    Interleave with even distribution based on lengths.
    
    Args:
        iterables: Sequence of iterables to interleave
        lengths: Optional sequence of lengths for distribution
        
    Returns:
        Iterator with evenly distributed items
    """

def roundrobin(*iterables):
    """
    Round-robin tournament scheduling of iterables.
    
    Args:
        *iterables: Variable number of iterables
        
    Returns:
        Iterator yielding items in round-robin order
    """

Usage Examples:

from more_itertools import interleave, roundrobin

# Basic interleaving
result = list(interleave([1, 2, 3], ['a', 'b', 'c']))
# Result: [1, 'a', 2, 'b', 3, 'c']

# Multiple iterables
result = list(interleave([1, 2], ['a', 'b'], ['x', 'y']))
# Result: [1, 'a', 'x', 2, 'b', 'y']

# Round-robin (alias for interleave)
result = list(roundrobin('ABC', '12'))
# Result: ['A', '1', 'B', '2', 'C']

Zip Operations

Enhanced zip operations with special behaviors.

def zip_equal(*iterables):
    """
    Zip iterables ensuring they have equal lengths.
    
    Args:
        *iterables: Iterables to zip together
        
    Returns:
        Iterator of tuples
        
    Raises:
        UnequalIterablesError: If iterables have different lengths
    """

def zip_offset(*iterables, offsets=None, longest=False, fillvalue=None):
    """
    Zip iterables with specified offsets.
    
    Args:
        *iterables: Iterables to zip
        offsets: Sequence of offset values for each iterable
        longest: If True, continue until longest is exhausted
        fillvalue: Value for padding when longest=True
        
    Returns:
        Iterator of offset-aligned tuples
    """

def zip_broadcast(*objects, scalar_types=(str, bytes), strict=False):
    """
    Zip with broadcasting of scalar values.
    
    Args:
        *objects: Objects to zip (iterables and scalars)
        scalar_types: Types considered as scalars
        strict: If True, require equal lengths for iterables
        
    Returns:
        Iterator with broadcasted values
    """

Augmenting Operations

Add elements or modify iterables without removing items.

def intersperse(element, iterable, n=1):
    """
    Insert element between items in iterable.
    
    Args:
        element: Element to insert
        iterable: Input iterable
        n: Insert after every n items
        
    Returns:
        Iterator with element interspersed
    """

def padded(iterable, fillvalue=None, n=None, next_multiple=False):
    """
    Pad iterable to specified length.
    
    Args:
        iterable: Input iterable
        fillvalue: Value to use for padding
        n: Target length (optional)
        next_multiple: If True, pad to next multiple of len
        
    Returns:
        Iterator padded to specified length
    """

def repeat_each(iterable, n=2):
    """
    Repeat each item n times.
    
    Args:
        iterable: Input iterable
        n: Number of times to repeat each item
        
    Returns:
        Iterator with each item repeated n times
    """

def repeat_last(iterable, default=None):
    """
    Repeat the last item indefinitely.
    
    Args:
        iterable: Input iterable
        default: Value to repeat if iterable is empty
        
    Returns:
        Iterator that repeats last item forever
    """

def prepend(value, iterator):
    """
    Prepend a value to an iterator.
    
    Args:
        value: Value to prepend
        iterator: Iterator to prepend to
        
    Returns:
        Iterator with value prepended
    """

Usage Examples:

from more_itertools import intersperse, repeat_each, prepend

# Intersperse separator
result = list(intersperse(',', ['a', 'b', 'c']))
# Result: ['a', ',', 'b', ',', 'c']

# Repeat each item
result = list(repeat_each([1, 2, 3], 2))
# Result: [1, 1, 2, 2, 3, 3]

# Prepend value
result = list(prepend('start', [1, 2, 3]))
# Result: ['start', 1, 2, 3]

Chaining and Value Operations

Combine values and iterables in flexible ways.

def value_chain(*args):
    """
    Chain values and iterables together.
    
    Args:
        *args: Mix of values and iterables to chain
        
    Returns:
        Iterator chaining all values and iterable contents
    """

Usage Examples:

from more_itertools import value_chain

# Chain values and iterables
result = list(value_chain(1, [2, 3], 4, [5, 6]))
# Result: [1, 2, 3, 4, 5, 6]

Count and Cycle Operations

Counting and cycling augmentation.

def count_cycle(iterable, n=None):
    """
    Cycle through iterable while counting occurrences.
    
    Args:
        iterable: Input iterable to cycle
        n: Maximum number of cycles (None for infinite)
        
    Returns:
        Iterator of (count, item) tuples
    """

def ncycles(iterable, n):
    """
    Repeat iterable n times.
    
    Args:
        iterable: Input iterable to repeat
        n: Number of repetitions
        
    Returns:
        Iterator repeating iterable n times
    """

Side Effects

Apply side effects while iterating.

def side_effect(func, iterable, chunk_size=None, before=None, after=None):
    """
    Apply side effect function while iterating.
    
    Args:
        func: Side effect function to apply
        iterable: Input iterable
        chunk_size: Process in chunks of this size
        before: Function to call before processing
        after: Function to call after processing
        
    Returns:
        Iterator yielding original items while applying side effects
    """

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