Iteration Utilities
iteration_utilities is a general-purpose collection of tools for functional programming based on Python's iterators and generators. It provides a wide array of functions, many inspired by the `itertools` module's recipes and the `toolz` package. Large fractions of its code are implemented in C for optimized performance. The library is under ongoing development, with the current stable version being 0.13.0, and its API may change in future releases.
Warnings
- breaking The library is under ongoing development, and its API is subject to change. Functions or their signatures may be altered in future versions.
- breaking Support for older Python versions is periodically dropped. Python 3.5 and 3.6 compatibility was removed in version 0.12.0. Ensure your Python environment meets the 'Python >=3.7' requirement.
- deprecated The top-level `__version__` property was removed. To get the library's version, use `importlib.metadata.version` from the Python standard library.
- gotcha Some functions, notably `groupedby`, may experience slightly reduced performance in Python 3.13 due to changes in optimizations.
- breaking In version 0.8.0, the `partial` function (if used) began allowing only plain string keyword-names in CPython 3.8. Additionally, private constants (e.g., `EQ_PY2`, `GE_PY3`) were removed from the module namespace.
Install
-
pip install iteration-utilities
Imports
- Iterable
from iteration_utilities import Iterable
- deepflatten
from iteration_utilities import deepflatten
Quickstart
from iteration_utilities import Iterable, duplicates, all_distinct
# Example 1: Chaining operations with Iterable
data = [1, 2, 2, 3, 4, 4, 5, 5, 5]
processed_data = Iterable(data).filter(lambda x: x % 2 == 0).unique_everseen().as_list()
print(f"Processed even unique data: {processed_data}")
# Example 2: Using a standalone function like duplicates
duplicate_items = list(duplicates(data))
print(f"Duplicate items: {duplicate_items}")
# Example 3: Using all_distinct
is_all_distinct = all_distinct([1, 2, 3, 4])
is_not_distinct = all_distinct([1, 2, 2, 3])
print(f"[1, 2, 3, 4] all distinct? {is_all_distinct}")
print(f"[1, 2, 2, 3] all distinct? {is_not_distinct}")