Itertools

This module contains various useful functions for working with iterators beyond the scope the itertools library without overlapping with more_itertools.

aiuti.itertools.exhaust(iterable: Iterable[Any]) None

Iterate through all the values of an iterable. This is useful for applying a mapped function which doesn’t return a useful value without writing a dedicated for loop.

>>> exhaust(map(print, range(3)))
0
1
2
aiuti.itertools.split(iterable: Iterable[T], condition: Union[Iterable[bool], Callable[[T], bool]]) Tuple[Iterator[T], Iterator[T]]

Similar to itertools.compress() except that it returns two iterators: one where with the values where the condition is True and one where with the values where the condition is False.

>>> from itertools import cycle
>>> evens, odds = split(range(5), cycle((True, False)))
>>> list(evens)
[0, 2, 4]
>>> list(odds)
[1, 3]

The condition can also be a boolean callable:

>>> evens, odds = split(range(5), lambda x: x % 2 == 0)
>>> list(evens)
[0, 2, 4]
>>> list(odds)
[1, 3]
Parameters:
  • iterable – Iterable to split into two sub-iterators. If given as iterator, it must not be advanced directly later!

  • condition – Either a boolean callable to map to each element or a boolean iterable indicating which values belong in which set. If given as iterator, it must not be advanced directly later!

Returns:

Tuple of two iterators: (where_true, where_false)