Additional building blocks, recipes, and routines for working with Python iterables beyond itertools.
—
Functions for handling unique elements and finding duplicates.
Remove duplicates and find unique elements.
def unique_everseen(iterable, key=None):
"""
Yield unique items preserving order of first occurrence.
Args:
iterable: Input iterable
key: Optional key function for uniqueness comparison
Returns:
Iterator of unique items in original order
"""
def unique_justseen(iterable, key=None):
"""
Remove consecutive duplicate items.
Args:
iterable: Input iterable
key: Optional key function for comparison
Returns:
Iterator with consecutive duplicates removed
"""
def unique(iterable, key=None, reverse=False):
"""
Remove duplicates from sorted iterable.
Args:
iterable: Input iterable (should be sorted)
key: Optional key function for comparison
reverse: If True, reverse the order
Returns:
Iterator of unique items
"""Usage Examples:
from more_itertools import unique_everseen, unique_justseen
# Preserve order, remove all duplicates
result = list(unique_everseen([1, 2, 2, 3, 1, 4]))
# Result: [1, 2, 3, 4]
# Remove only consecutive duplicates
result = list(unique_justseen([1, 1, 2, 2, 3, 1, 1]))
# Result: [1, 2, 3, 1]
# With key function
result = list(unique_everseen(['a', 'A', 'b', 'B'], key=str.lower))
# Result: ['a', 'b']Find and extract duplicate elements.
def duplicates_everseen(iterable, key=None):
"""
Yield duplicate items preserving order of first duplication.
Args:
iterable: Input iterable
key: Optional key function for comparison
Returns:
Iterator of items that appear more than once
"""
def duplicates_justseen(iterable, key=None):
"""
Yield consecutive duplicate items.
Args:
iterable: Input iterable
key: Optional key function for comparison
Returns:
Iterator of consecutive duplicate items
"""Usage Examples:
from more_itertools import duplicates_everseen, duplicates_justseen
# Find all duplicates
duplicates = list(duplicates_everseen([1, 2, 2, 3, 1, 4, 2]))
# Result: [2, 1, 2] (order of first duplication)
# Find consecutive duplicates
consecutive_dups = list(duplicates_justseen([1, 1, 2, 3, 3, 3, 4]))
# Result: [1, 3, 3]Test and classify uniqueness properties.
def all_unique(iterable, key=None):
"""
Check if all items in iterable are unique.
Args:
iterable: Input iterable to test
key: Optional key function for comparison
Returns:
True if all items are unique, False otherwise
"""
def classify_unique(iterable, key=None):
"""
Classify items by their uniqueness.
Args:
iterable: Input iterable
key: Optional key function for comparison
Returns:
Tuple of (unique_items, duplicated_items) lists
"""Usage Examples:
from more_itertools import all_unique, classify_unique
# Test uniqueness
print(all_unique([1, 2, 3, 4])) # True
print(all_unique([1, 2, 2, 3])) # False
# Classify by uniqueness
unique_items, duplicate_items = classify_unique([1, 2, 2, 3, 4, 4, 5])
print(unique_items) # [1, 3, 5]
print(duplicate_items) # [2, 4]Operations for uniqueness across multiple iterables.
def unique_to_each(iterables):
"""
Find items unique to each iterable.
Args:
iterables: Sequence of iterables to compare
Returns:
Iterator of sets, each containing items unique to corresponding iterable
"""
def unique_in_window(iterable, n, key=None):
"""
Yield items unique within sliding window.
Args:
iterable: Input iterable
n: Window size
key: Optional key function for comparison
Returns:
Iterator of items that are unique within their window
"""Usage Examples:
from more_itertools import unique_to_each, unique_in_window
# Find items unique to each list
lists = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
unique_sets = list(unique_to_each(lists))
# Result: [{1}, {2}, {5}]
# Unique within sliding window
result = list(unique_in_window([1, 2, 1, 3, 2, 4], 3))
# Result: Items unique within each 3-item windowInstall with Tessl CLI
npx tessl i tessl/pypi-more-itertools