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

sequence-utilities.mddocs/

Sequence Utilities

Functions specifically for working with sequences and sequence-like objects.

Capabilities

Type Conversion and Validation

Functions for ensuring inputs are in expected formats.

def always_iterable(obj: Any, base_type: type = (str, bytes)) -> Iterable[Any]: ...  
def always_reversible(iterable: Iterable[Any]) -> Reversible[Any]: ...

Usage:

from more_itertools import always_iterable, always_reversible

# Ensure object is always iterable
always_iterable(42)        # [42]
always_iterable([1, 2])    # [1, 2]
always_iterable("hello")   # ["hello"] (string treated as single item)

# Ensure object is reversible
rev = always_reversible([1, 2, 3])
list(reversed(rev))  # [3, 2, 1]

Sequence Properties and Analysis

Functions for analyzing sequence characteristics.

def adjacent(iterable: Iterable[Any], predicate: Callable[[Any, Any], bool]) -> bool: ...
def is_sorted(iterable: Iterable[Any], *, key: Callable[[Any], Any] = None, reverse: bool = False, strict: bool = False) -> bool: ...
def longest_common_prefix(iterables: Iterable[Iterable[Any]]) -> list[Any]: ...

Usage:

from more_itertools import adjacent, is_sorted, longest_common_prefix

# Check if any adjacent elements satisfy predicate
adjacent([1, 2, 3, 5], lambda a, b: b - a > 1)  # True (3, 5)

# Check if sequence is sorted
is_sorted([1, 2, 3, 4])      # True
is_sorted([1, 3, 2, 4])      # False
is_sorted([4, 3, 2, 1], reverse=True)  # True

# Find common prefix across multiple sequences
longest_common_prefix([
    [1, 2, 3, 4],
    [1, 2, 5, 6], 
    [1, 2, 7, 8]
])  # [1, 2]

Sequence Marking and Annotation

Functions for marking special positions in sequences.

def mark_ends(iterable: Iterable[Any]) -> Iterator[tuple[bool, bool, Any]]: ...

Usage:

from more_itertools import mark_ends

# Mark first and last elements
data = ['a', 'b', 'c', 'd']
marked = list(mark_ends(data))
# [(True, False, 'a'), (False, False, 'b'), (False, False, 'c'), (False, True, 'd')]
# (is_first, is_last, value)

Element Replacement and Modification

Functions for modifying sequence elements.

def replace(iterable: Iterable[Any], pred: Callable[[Any], bool], substitutes: Iterable[Any], *, count: int = None, window_size: int = 1) -> Iterator[Any]: ...

Usage:

from more_itertools import replace

# Replace elements matching predicate
data = [1, 2, 3, 2, 4, 2, 5]
result = list(replace(data, lambda x: x == 2, [10, 20]))
# [1, 10, 20, 3, 10, 20, 4, 10, 20, 5]

# Limit number of replacements
result = list(replace(data, lambda x: x == 2, [99], count=2))
# [1, 99, 3, 99, 4, 2, 5]

Sequence Trimming

Functions for removing elements from sequence ends.

def strip(iterable: Iterable[Any], pred: Callable[[Any], bool]) -> Iterator[Any]: ...
def lstrip(iterable: Iterable[Any], pred: Callable[[Any], bool]) -> Iterator[Any]: ...
def rstrip(iterable: Iterable[Any], pred: Callable[[Any], bool]) -> Iterator[Any]: ...

Usage:

from more_itertools import strip, lstrip, rstrip

data = [0, 0, 1, 2, 3, 0, 0]

# Strip from both ends
list(strip(data, lambda x: x == 0))   # [1, 2, 3]

# Strip from left only
list(lstrip(data, lambda x: x == 0))  # [1, 2, 3, 0, 0]

# Strip from right only  
list(rstrip(data, lambda x: x == 0))  # [0, 0, 1, 2, 3]

Multi-Sequence Operations

Functions for operating on multiple sequences together.

def sort_together(iterables: Iterable[Iterable[Any]], *, key_list: list[Callable[[Any], Any]] = None, reverse: bool = False) -> list[tuple[Any, ...]]: ...

Usage:

from more_itertools import sort_together

# Sort multiple sequences based on first sequence
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 20]
scores = [85, 90, 95]

sorted_data = sort_together([names, ages, scores])
# [('Alice', 25, 85), ('Bob', 30, 90), ('Charlie', 20, 95)]

# Sort by ages (second sequence)
sorted_by_age = sort_together([names, ages, scores], key_list=[None, lambda x: x, None])

Sequence Views

Classes for creating views of sequences.

class SequenceView:
    def __init__(self, target: Sequence[Any]) -> None: ...
    def __getitem__(self, index: int | slice) -> Any: ...
    def __len__(self) -> int: ...

Usage:

from more_itertools import SequenceView

# Create a view of a sequence
data = [1, 2, 3, 4, 5]
view = SequenceView(data)

# View behaves like the original sequence
view[0]     # 1
view[1:3]   # [2, 3]
len(view)   # 5

# But changes to original are reflected
data.append(6)
len(view)   # 6

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