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

comparison.mddocs/

Comparison and Equality Operations

Functions for comparing and testing equality between iterables.

Capabilities

Iterator Equality Testing

Functions for testing equality between iterators and iterables.

def iequals(*iterables: Iterable[Any]) -> bool: ...

Usage:

from more_itertools import iequals

# Test if multiple iterables produce the same elements
iequals([1, 2, 3], (1, 2, 3), iter([1, 2, 3]))  # True
iequals([1, 2, 3], [1, 2, 4])                   # False

# Works with generators and any iterables
def gen1():
    yield from [1, 2, 3]

def gen2():
    yield from [1, 2, 3]

iequals(gen1(), gen2())  # True

# Different lengths
iequals([1, 2, 3], [1, 2])  # False

Difference Detection

Functions for finding differences between iterables.

def difference(iterable: Iterable[Any], *others: Iterable[Any]) -> Iterator[Any]: ...

Usage:

from more_itertools import difference

# Find elements in first iterable not in others
first = [1, 2, 3, 4, 5]
second = [3, 4, 5, 6, 7]
third = [5, 6, 7, 8, 9]

diff = list(difference(first, second, third))  # [1, 2]

# Order matters - finds elements in first that aren't in any of the others
diff2 = list(difference(second, first))  # [6, 7]

# With strings
text1 = "hello world"
text2 = "world hello"
char_diff = ''.join(difference(text1, text2))  # "" (same characters)

text3 = "hello world!"
char_diff2 = ''.join(difference(text3, text1))  # "!"

Conditional Splitting

Functions for splitting iterables based on predicates.

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

Usage:

from more_itertools import before_and_after

# Split iterable at first element matching predicate
data = [1, 2, 3, 10, 4, 5, 6]

before, after = before_and_after(lambda x: x >= 10, data)

before_list = list(before)  # [1, 2, 3]
after_list = list(after)    # [10, 4, 5, 6]

# With strings - split at first vowel
text = "programming"
before_vowel, from_vowel = before_and_after(lambda c: c in 'aeiou', text)

print(''.join(before_vowel))  # "pr"
print(''.join(from_vowel))    # "ogramming"

# If predicate never matches, before gets everything, after is empty
no_match_data = [1, 2, 3, 4]
before, after = before_and_after(lambda x: x > 10, no_match_data)

list(before)  # [1, 2, 3, 4]
list(after)   # []

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