A Few Useful Iterators

Note the iterator “gray_product” that used to be in this module has been merged into more_itertools as of its version 9.1.0 as gray_product().

igbpyutils.iter.zip_strict = functools.partial(<class 'zip'>, strict=True)

Just an alias for zip() with strict=True, kept for backwards compatibility. May be removed in a future version.

class igbpyutils.iter.SizedCallbackIterator(it: Iterable[_T], length: int, *, strict: bool = False, callback: Callable[[int, _T], None] | None = None)[source]

Wrapper to add len() support and a callback to an iterator.

For example, this can be used to wrap a generator which has a known output length (e.g. if it returns exactly one item per input item), so that it can then be used in libraries like tqdm.

igbpyutils.iter.is_unique_everseen(iterable: Iterable[_V], *, key: Callable[[_V], Any] | None = None) Generator[bool, None, None][source]

For each element in the input iterable, return either True if this element is unique, or False if it is not.

Deprecated since version 0.5.0: Use more_itertools.classify_unique() instead.

The implementation is very similar more_itertools.unique_everseen() and is subject to the same performance considerations.

igbpyutils.iter.no_duplicates(iterable: Iterable[_V], *, key: Callable[[_V], Any] | None = None, name: str = 'item') Generator[_V, None, None][source]

Raise a ValueError if there are any duplicate elements in the input iterable.

Remember that if you don’t want to use this iterator’s return values, but only use it for checking a list, you need to force it to execute by wrapping the call e.g. in a set or list. Alternatively, use not all( ever for e,just,ever in classify_unique(iterable) ).

The name argument is only to customize the error messages.

more_itertools.duplicates_everseen() could also be used for this purpose, but this function returns the values of the input iterable.

The implementation is very similar more_itertools.unique_everseen() and is subject to the same performance considerations.