py_itertools¶
shortfx.fxPython.py_itertools
¶
shortfx - fxPython: Itertools Recipes Module
This module provides advanced iterator utility functions based on Python's itertools documentation recipes. It includes functions for: - Iterator manipulation (take, prepend, flatten, sliding_window) - Sequence generation (tabulate, repeatfunc, ncycles) - Unique element filtering (unique_justseen, unique_everseen) - Grouping and batching (grouper, roundrobin, batched) - Mathematical operations (polynomial_eval, matmul, convolve) - Prime number utilities (sieve, factor, is_prime, totient)
All functions are optimized for memory efficiency using lazy evaluation where possible. Based on: https://docs.python.org/3/library/itertools.html
Functions¶
all_equal(iterable, key=None)
¶
Description: Returns True if all the elements are equal to each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required | |
key
|
Key function for comparison. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if all equal. |
Usage Example
all_equal([1,1,1]) -> True
Source code in shortfx/fxPython/py_itertools.py
consume(iterator, n=None)
¶
Description: Advance the iterator n-steps ahead. If n is None, consume entirely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterator
|
The iterator to consume. |
required | |
n
|
int or None
|
Number of steps or None. |
None
|
Returns:
| Type | Description |
|---|---|
|
None |
Usage Example
consume(iter([1,2,3]), 2) # advances by 2
Source code in shortfx/fxPython/py_itertools.py
convolve(signal, kernel)
¶
Description: Discrete linear convolution of two iterables. Equivalent to polynomial multiplication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
The signal iterable. |
required | |
kernel
|
The kernel iterable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Convolution result. |
Usage Example
list(convolve([1, -1, -20], [1, -3])) -> [1, -4, -17, 60]
Source code in shortfx/fxPython/py_itertools.py
factor(n)
¶
Description: Prime factors of n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number to factor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of prime factors. |
Usage Example
list(factor(99)) -> [3, 3, 11]
Source code in shortfx/fxPython/py_itertools.py
first_true(iterable, default=False, predicate=None)
¶
Description: Returns the first true value or the default if there is no true value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required | |
default
|
Default value. |
False
|
|
predicate
|
Function to check truthiness. |
None
|
Returns:
| Type | Description |
|---|---|
|
First true value or default. |
Usage Example
first_true([0,1,2], predicate=bool) -> 1
Source code in shortfx/fxPython/py_itertools.py
flatten(list_of_lists)
¶
Description: Flatten one level of nesting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_of_lists
|
Iterable of iterables to flatten. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Flattened iterator. |
Usage Example
list(flatten([[1,2], [3,4]])) -> [1,2,3,4]
Source code in shortfx/fxPython/py_itertools.py
grouper(iterable, n, *, incomplete='fill', fillvalue=None)
¶
Description: Collect data into non-overlapping fixed-length chunks or blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required | |
n
|
int
|
Group size. |
required |
incomplete
|
How to handle incomplete groups ('fill', 'strict', 'ignore'). |
'fill'
|
|
fillvalue
|
Value to fill with. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of tuples. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If incomplete is not 'fill', 'strict', or 'ignore'. |
Usage Example
list(grouper('ABCDEFG', 3, fillvalue='x')) -> [('A','B','C'), ('D','E','F'), ('G','x','x')]
Source code in shortfx/fxPython/py_itertools.py
iter_except(function, exception, first=None)
¶
Description: Convert a call-until-exception interface to an iterator interface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Function to call. |
required | |
exception
|
Exception to catch. |
required | |
first
|
Initial value. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator from function calls. |
Usage Example
list(iter_except(dict.popitem, KeyError)) # from a dict
Source code in shortfx/fxPython/py_itertools.py
iter_index(iterable, value, start=0, stop=None)
¶
Description: Return indices where a value occurs in a sequence or iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required | |
value
|
Value to find. |
required | |
start
|
int
|
Start index. |
0
|
stop
|
int or None
|
Stop index. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of indices. |
Usage Example
list(iter_index('AABCADEAF', 'A')) -> [0, 1, 4, 7]
Source code in shortfx/fxPython/py_itertools.py
loops(n)
¶
Description: Loop n times. Like range(n) but without creating integers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of loops. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of None values. |
Usage Example
for _ in loops(5): print("loop")
Source code in shortfx/fxPython/py_itertools.py
matmul(m1, m2)
¶
Description: Multiply two matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m1
|
First matrix. |
required | |
m2
|
Second matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Result matrix. |
Usage Example
list(matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)])) -> [(49, 80), (41, 60)]
Source code in shortfx/fxPython/py_itertools.py
multinomial(*counts)
¶
Description: Number of distinct arrangements of a multiset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*counts
|
Counts of each item. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
Multinomial coefficient. |
Usage Example
multinomial(5, 2, 2, 1, 1) -> 83160
Source code in shortfx/fxPython/py_itertools.py
ncycles(iterable, n)
¶
Description: Returns the sequence elements n times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable to cycle. |
required | |
n
|
int
|
Number of cycles. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator repeating the sequence n times. |
Usage Example
list(ncycles([1,2], 3)) -> [1,2,1,2,1,2]
Source code in shortfx/fxPython/py_itertools.py
nth(iterable, n, default=None)
¶
Description: Returns the nth item or a default value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required | |
n
|
int
|
Index of item. |
required |
default
|
Default value if not found. |
None
|
Returns:
| Type | Description |
|---|---|
|
The nth item or default. |
Usage Example
nth([1,2,3], 1) -> 2
Source code in shortfx/fxPython/py_itertools.py
polynomial_derivative(coefficients)
¶
Description: Compute the first derivative of a polynomial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coefficients
|
List of coefficients. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
Derivative coefficients. |
Usage Example
polynomial_derivative([1, -4, -17, 60]) -> [3, -8, -17]
Source code in shortfx/fxPython/py_itertools.py
polynomial_eval(coefficients, x)
¶
Description: Evaluate a polynomial at a specific value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coefficients
|
List of coefficients. |
required | |
x
|
Value to evaluate at. |
required |
Returns:
| Type | Description |
|---|---|
|
float or int: Result. |
Usage Example
polynomial_eval([1, -4, -17, 60], x=5) -> 0
Source code in shortfx/fxPython/py_itertools.py
polynomial_from_roots(roots)
¶
Description: Compute a polynomial's coefficients from its roots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roots
|
List of roots. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
Coefficients. |
Usage Example
polynomial_from_roots([5, -4, 3]) -> [1, -4, -17, 60]
Source code in shortfx/fxPython/py_itertools.py
powerset(iterable)
¶
Description: Subsequences of the iterable from shortest to longest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of tuples. |
Usage Example
list(powerset([1,2,3])) -> [(), (1,), (2,), (3,), (1,2), (1,3), (2,3), (1,2,3)]
Source code in shortfx/fxPython/py_itertools.py
prepend(value, iterable)
¶
Description: Prepend a single value in front of an iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
The value to prepend. |
required | |
iterable
|
The iterable to prepend to. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator with value prepended. |
Usage Example
list(prepend(1, [2, 3, 4])) -> [1, 2, 3, 4]
Cost: O(1) for creation, O(n) when consumed.
Source code in shortfx/fxPython/py_itertools.py
quantify(iterable, predicate=bool)
¶
Description: Given a predicate that returns True or False, count the True results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable to check. |
required | |
predicate
|
Function to apply. |
bool
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
Count of True results. |
Usage Example
quantify([1,2,3,4], lambda x: x > 2) -> 2
Source code in shortfx/fxPython/py_itertools.py
repeatfunc(function, times=None, *args)
¶
Description: Repeat calls to a function with specified arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
The function to call. |
required | |
times
|
int or None
|
Number of times to repeat, or None for infinite. |
None
|
*args
|
Arguments to pass to the function. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of function results. |
Usage Example
list(repeatfunc(lambda: random.random(), 3))
Cost: O(1) per function call.
Source code in shortfx/fxPython/py_itertools.py
reshape(matrix, columns)
¶
Description: Reshape a 2-D matrix to have a given number of columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
2D matrix. |
required | |
columns
|
int
|
Number of columns. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Reshaped matrix. |
Usage Example
list(reshape([(0, 1), (2, 3), (4, 5)], 3)) -> [(0, 1, 2), (3, 4, 5)]
Source code in shortfx/fxPython/py_itertools.py
roundrobin(*iterables)
¶
Description: Visit input iterables in a cycle until each is exhausted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*iterables
|
The iterables. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Round-robin iterator. |
Usage Example
list(roundrobin('ABC', 'D', 'EF')) -> ['A', 'D', 'E', 'B', 'F', 'C']
Source code in shortfx/fxPython/py_itertools.py
sieve(n)
¶
Description: Primes less than n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Upper limit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of primes. |
Usage Example
list(sieve(30)) -> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Source code in shortfx/fxPython/py_itertools.py
sliding_window(iterable, n)
¶
Description: Collect data into overlapping fixed-length chunks or blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required | |
n
|
int
|
Window size. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of tuples. |
Usage Example
list(sliding_window('ABCDEFG', 4)) -> [('A','B','C','D'), ('B','C','D','E'), ...]
Source code in shortfx/fxPython/py_itertools.py
subslices(seq)
¶
Description: Return all contiguous non-empty subslices of a sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq
|
The sequence. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of slices. |
Usage Example
list(subslices('ABCD')) -> ['A', 'AB', 'ABC', 'ABCD', 'B', 'BC', 'BCD', 'C', 'CD', 'D']
Source code in shortfx/fxPython/py_itertools.py
tabulate(function, start=0)
¶
Description: Return function(0), function(1), ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
The function to apply. |
required | |
start
|
int
|
Starting index. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of function applied to consecutive integers. |
Usage Example
list(tabulate(lambda x: x**2, 3)) -> [9, 16, 25, ...]
Cost: O(1) per element generated.
Source code in shortfx/fxPython/py_itertools.py
tail(n, iterable)
¶
Description: Return an iterator over the last n items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of last items. |
required |
iterable
|
The iterable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Iterator of last n items. |
Usage Example
list(tail(3, 'ABCDEFG')) -> ['E', 'F', 'G']
Source code in shortfx/fxPython/py_itertools.py
take(n, iterable)
¶
Description: Return first n items of the iterable as a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of items to take. |
required |
iterable
|
The iterable to take from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
List of first n items. |
Usage Example
take(3, [1,2,3,4,5]) -> [1,2,3]
Cost: O(n), where n is the number of items to take.
Source code in shortfx/fxPython/py_itertools.py
totient(n)
¶
Description: Count of natural numbers up to n that are coprime to n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
Totient value. |
Usage Example
totient(12) -> 4
Source code in shortfx/fxPython/py_itertools.py
transpose(matrix)
¶
Description: Swap the rows and columns of a 2-D matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
2D matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Transposed matrix. |
Usage Example
list(transpose([(1, 2, 3), (11, 22, 33)])) -> [(1, 11), (2, 22), (3, 33)]
Source code in shortfx/fxPython/py_itertools.py
unique(iterable, key=None, reverse=False)
¶
Description: Yield unique elements in sorted order. Supports unhashable inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required | |
key
|
Key function. |
None
|
|
reverse
|
bool
|
Sort in reverse. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Unique sorted elements. |
Usage Example
list(unique([[1, 2], [3, 4], [1, 2]])) -> [[1, 2], [3, 4]]
Source code in shortfx/fxPython/py_itertools.py
unique_everseen(iterable, key=None)
¶
Description: Yield unique elements, preserving order. Remember all elements ever seen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required | |
key
|
Key function. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Unique elements. |
Usage Example
list(unique_everseen('AAAABBBCCDAABBB')) -> ['A', 'B', 'C', 'D']
Source code in shortfx/fxPython/py_itertools.py
unique_justseen(iterable, key=None)
¶
Description: Yield unique elements, preserving order. Remember only the element just seen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The iterable. |
required | |
key
|
Key function. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Unique elements. |
Usage Example
list(unique_justseen('AAAABBBCCDAABBB')) -> ['A', 'B', 'C', 'D', 'A', 'B']