Additional building blocks, recipes, and routines for working with Python iterables beyond itertools.
npx @tessl/cli install tessl/pypi-more-itertools@10.8.0More-itertools provides an extensive collection of utility functions for working with Python iterables that extend beyond the standard itertools library. It offers over 150 carefully designed functions organized into categories including grouping operations, windowing functions, combining and augmenting iterables, summarizing tools, selecting and filtering functions, mathematical operations, combinatorics utilities, and various wrapping and utility functions.
pip install more-itertoolsimport more_itertoolsFor specific functions:
from more_itertools import chunked, windowed, peekable, take, unique_everseenfrom more_itertools import chunked, windowed, peekable, take, first
# Break data into chunks
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunks = list(chunked(data, 3))
print(chunks) # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
# Create sliding windows
windows = list(windowed(data, 3))
print(windows) # [(1, 2, 3), (2, 3, 4), (3, 4, 5), ...]
# Peek ahead in an iterator
p = peekable(iter(data))
print(p.peek()) # 1 (without consuming)
print(next(p)) # 1 (now consumed)
# Take first n items
first_five = take(5, data)
print(first_five) # [1, 2, 3, 4, 5]
# Get first item safely
print(first(data)) # 1
print(first([], 'none')) # 'none'More-itertools is built around several key design principles:
The library is organized into two main modules:
Core functionality for breaking iterables into chunks, groups, and partitions.
def chunked(iterable, n, strict=False):
"""Break iterable into lists of length n"""
def batched(iterable, n, *, strict=False):
"""Group items into batches of size n"""
def grouper(iterable, n, incomplete='fill', fillvalue=None):
"""Group into fixed-length chunks with fillvalue"""
def partition(pred, iterable):
"""Partition into false and true groups"""Functions for creating sliding windows and overlapping views of data.
def windowed(iterable, n, fillvalue=None, step=1):
"""Create sliding window of size n"""
def pairwise(iterable):
"""Return successive overlapping pairs"""
def stagger(iterable, offsets=(0, 1), longest=False, fillvalue=None):
"""Create lagged iterables"""Advanced iterator wrappers that provide peek and seek capabilities.
class peekable:
"""Iterator that allows peeking ahead"""
def peek(self, default=None): ...
def prepend(self, *values): ...
class seekable:
"""Iterator that allows seeking to positions"""
def seek(self, index): ...
def relative_seek(self, count): ...
def spy(iterable, n=1):
"""Return first n items and iterator"""Functions for extracting, filtering, and selecting elements from iterables.
def take(n, iterable):
"""Return first n items as list"""
def first(iterable, default=None):
"""Return first item or default"""
def nth(iterable, n, default=None):
"""Return nth item or default"""
def filter_except(function, iterable, *exceptions):
"""Filter with exception handling"""Functions for merging, combining, and augmenting iterables.
def interleave(*iterables):
"""Round-robin interleaving"""
def zip_equal(*iterables):
"""Zip ensuring equal lengths"""
def intersperse(element, iterable):
"""Insert element between items"""
def prepend(value, iterator):
"""Prepend value to iterator"""Mathematical computations and operations on iterables.
def dotproduct(vec1, vec2):
"""Compute dot product"""
def convolve(signal, kernel):
"""Compute convolution"""
def polynomial_eval(coefficients, x):
"""Evaluate polynomial at x"""
def sum_of_squares(iterable):
"""Sum the squares of items"""
def nth_prime(n, *, approximate=False):
"""Return nth prime number (0-indexed)"""Functions for generating permutations, combinations, and related structures.
def powerset(iterable):
"""Generate powerset of items"""
def distinct_combinations(iterable, r):
"""Combinations of distinct items"""
def partitions(n):
"""Integer partitions of n"""
def circular_shifts(iterable):
"""All circular shifts"""Functions for handling unique elements and finding duplicates.
def unique_everseen(iterable, key=None):
"""Unique items preserving order"""
def duplicates_everseen(iterable, key=None):
"""Duplicate items preserving order"""
def all_unique(iterable, key=None):
"""Check if all items unique"""Functions that compute summaries and statistics over iterables.
def ilen(iterable):
"""Length of iterable"""
def quantify(iterable, pred=bool):
"""Count matching items"""
def minmax(iterable, *, default=None, key=None):
"""Find min and max in single pass"""
def all_equal(iterable):
"""Check if all elements equal"""Functions specifically for working with sequences and sequence-like objects.
def always_iterable(obj, base_type=(str, bytes)):
"""Ensure input is iterable"""
def is_sorted(iterable, *, key=None, reverse=False, strict=False):
"""Check if sequence is sorted"""
def mark_ends(iterable):
"""Mark first and last elements"""
def sort_together(iterables, *, key_list=None, reverse=False):
"""Sort multiple sequences together"""Functions for working with indices and positions in sequences.
def combination_index(element, pool):
"""Get index of combination"""
def nth_combination(iterable, r, index):
"""Get combination at index"""
def permutation_index(element, pool):
"""Get index of permutation"""
def iter_index(iterable, value, *, start=0, stop=None):
"""Find all indices of value"""Functions for generating random selections and samples.
def random_product(*iterables, repeat=1):
"""Random Cartesian product element"""
def random_combination(iterable, r):
"""Random combination"""
def sample(iterable, k, *, counts=None):
"""Sample k elements"""Core iteration utilities and helpers for advanced patterns.
def iterate(func, value):
"""Generate sequence by applying function"""
def tabulate(func, start=0):
"""Tabulate function results"""
def iter_except(func, exception, first=None):
"""Iterate until exception"""Classes and decorators providing specialized functionality.
class countable:
"""Iterator wrapper that tracks items seen"""
def items_seen(self): ...
def consumer(func):
"""Decorator for coroutine consumers"""
def raise_(exception):
"""Utility for raising exceptions in expressions"""Functions for comparing and testing equality between iterables.
def iequals(*iterables):
"""Test if iterables produce same elements"""
def difference(iterable, *others):
"""Elements in first not in others"""
def before_and_after(predicate, iterable):
"""Split at first matching element"""Specialized utilities for complex operations.
def flatten(iterable):
"""Flatten one level of nesting"""
def collapse(iterable, base_type=None, levels=None):
"""Deep flattening of nested structures"""
def map_reduce(iterable, keyfunc, valuefunc=None, reducefunc=None):
"""Map-reduce operation"""
class time_limited:
"""Time-limited iteration class"""
def interleave_randomly(*iterables):
"""Randomly interleave multiple iterables"""
def takewhile_inclusive(predicate, iterable):
"""Takewhile including first failing element"""
def with_iter(context_manager):
"""Context-managed iteration"""
def iter_suppress(iterable, *exceptions):
"""Exception suppression during iteration"""from collections.abc import Iterable, Iterator
from typing import TypeVar, Any, Callable, Optional
T = TypeVar('T')
U = TypeVar('U')
class UnequalIterablesError(ValueError):
"""Raised when iterables have unequal lengths"""
class SequenceView:
"""Provides view into sequence with lazy slicing"""
def __init__(self, target): ...
def __getitem__(self, index): ...
def __len__(self): ...
class bucket:
"""Dynamic bucketing of iterable items by key function"""
def __init__(self, iterable, key, validator=None): ...
def __contains__(self, value): ...
def __getitem__(self, value): ...
class callback_iter:
"""Convert callback-based function to iterator"""
def __init__(self, func, callback_kwd='callback', wait_seconds=0.1): ...
def __enter__(self): ...
def __exit__(self, exc_type, exc_value, traceback): ...
class run_length:
"""Run-length encoding and decoding operations"""
@staticmethod
def encode(iterable): ...
@staticmethod
def decode(iterable): ...
class islice_extended:
"""Extended islice with negative index support"""
def __init__(self, iterable, *args): ...
def __getitem__(self, key): ...
class numeric_range:
"""Extended range() for any numeric types"""
def __init__(self, *args): ...
def __contains__(self, value): ...