iterables

Operations on iterables.

TypeVar’s

class T
TypeVar('T')
class U
TypeVar('U')
class F
TypeVar('F', np.float64, np.int_)
class H
TypeVar('H', bound=Hashable)

Functions

adjacent_n_tuples(objects, n)[source]

Returns the Sequence objects cyclically split into n length tuples.

See also

adjacent_pairs

alias with n=2

Examples

>>> list(adjacent_n_tuples([1, 2, 3, 4], 2))
[(1, 2), (2, 3), (3, 4), (4, 1)]
>>> list(adjacent_n_tuples([1, 2, 3, 4], 3))
[(1, 2, 3), (2, 3, 4), (3, 4, 1), (4, 1, 2)]
Parameters:
  • objects (Sequence[T])

  • n (int)

Return type:

zip[tuple[T, …]]

adjacent_pairs(objects)[source]

Alias for adjacent_n_tuples(objects, 2).

Examples

>>> list(adjacent_pairs([1, 2, 3, 4]))
[(1, 2), (2, 3), (3, 4), (4, 1)]
Parameters:

objects (Sequence[T])

Return type:

zip[tuple[T, …]]

all_elements_are_instances(iterable, Class)[source]

Returns True if all elements of iterable are instances of Class. False otherwise.

Parameters:
  • iterable (Iterable[object])

  • Class (type[object])

Return type:

bool

batch_by_property(items, property_func)[source]

Takes in a Sequence, and returns a list of tuples, (batch, prop) such that all items in a batch have the same output when put into the Callable property_func, and such that chaining all these batches together would give the original Sequence (i.e. order is preserved).

Examples

>>> batch_by_property([(1, 2), (3, 4), (5, 6, 7), (8, 9)], len)
[([(1, 2), (3, 4)], 2), ([(5, 6, 7)], 3), ([(8, 9)], 2)]
Parameters:
  • items (Iterable[T])

  • property_func (Callable[[T], U])

Return type:

list[tuple[list[T], U | None]]

concatenate_lists(*list_of_lists)[source]

Combines the Iterables provided as arguments into one list.

Examples

>>> concatenate_lists([1, 2], [3, 4], [5])
[1, 2, 3, 4, 5]
Parameters:

list_of_lists (Iterable[T])

Return type:

list[T]

hash_obj(obj)[source]

Determines a hash, even of potentially mutable objects.

Parameters:

obj (object)

Return type:

int

list_difference_update(l1, l2)[source]

Returns a list containing all the elements of l1 not in l2.

Examples

>>> list_difference_update([1, 2, 3, 4], [2, 4])
[1, 3]
Parameters:
  • l1 (Iterable[T])

  • l2 (Iterable[T])

Return type:

list[T]

list_update(l1, l2)[source]
Used instead of set.update() to maintain order,

making sure duplicates are removed from l1, not l2. Removes overlap of l1 and l2 and then concatenates l2 unchanged.

Examples

>>> list_update([1, 2, 3], [2, 4, 4])
[1, 3, 2, 4, 4]
Parameters:
  • l1 (Iterable[T])

  • l2 (Iterable[T])

Return type:

list[T]

listify(obj: str) list[str][source]
listify(obj: Iterable[T]) list[T]
listify(obj: T) list[T]

Converts obj to a list intelligently.

Examples

>>> listify("str")
['str']
>>> listify((1, 2))
[1, 2]
>>> listify(len)
[<built-in function len>]
Parameters:

obj (str | Iterable[T] | T)

Return type:

list[str] | list[T]

make_even(iterable_1, iterable_2)[source]
Extends the shorter of the two iterables with duplicate values until its

length is equal to the longer iterable (favours earlier elements).

See also

make_even_by_cycling

cycles elements instead of favouring earlier ones

Examples

>>> make_even([1, 2], [3, 4, 5, 6])
([1, 1, 2, 2], [3, 4, 5, 6])

>>> make_even([1, 2], [3, 4, 5, 6, 7])
([1, 1, 1, 2, 2], [3, 4, 5, 6, 7])
Parameters:
  • iterable_1 (Iterable[T])

  • iterable_2 (Iterable[U])

Return type:

tuple[list[T], list[U]]

make_even_by_cycling(iterable_1, iterable_2)[source]
Extends the shorter of the two iterables with duplicate values until its

length is equal to the longer iterable (cycles over shorter iterable).

See also

make_even

favours earlier elements instead of cycling them

Examples

>>> make_even_by_cycling([1, 2], [3, 4, 5, 6])
([1, 2, 1, 2], [3, 4, 5, 6])

>>> make_even_by_cycling([1, 2], [3, 4, 5, 6, 7])
([1, 2, 1, 2, 1], [3, 4, 5, 6, 7])
Parameters:
  • iterable_1 (Collection[T])

  • iterable_2 (Collection[U])

Return type:

tuple[list[T], list[U]]

remove_list_redundancies(lst)[source]

Used instead of list(set(l)) to maintain order. Keeps the last occurrence of each element.

Parameters:

lst (Reversible[H])

Return type:

list[H]

remove_nones(sequence)[source]

Removes elements where bool(x) evaluates to False.

Examples

>>> remove_nones(["m", "", "l", 0, 42, False, True])
['m', 'l', 42, True]
Parameters:

sequence (Iterable[T | None])

Return type:

list[T]

resize_array(nparray, length)[source]
Extends/truncates nparray so that len(result) == length.

The elements of nparray are cycled to achieve the desired length.

See also

resize_preserving_order

favours earlier elements instead of cycling them

make_even_by_cycling

similar cycling behaviour for balancing 2 iterables

Examples

>>> points = np.array([[1, 2], [3, 4]])
>>> resize_array(points, 1)
array([[1, 2]])
>>> resize_array(points, 3)
array([[1, 2],
       [3, 4],
       [1, 2]])
>>> resize_array(points, 2)
array([[1, 2],
       [3, 4]])
Parameters:
  • nparray (npt.NDArray[F])

  • length (int)

Return type:

npt.NDArray[F]

resize_preserving_order(nparray, length)[source]
Extends/truncates nparray so that len(result) == length.

The elements of nparray are duplicated to achieve the desired length (favours earlier elements).

Constructs a zeroes array of length if nparray is empty.

See also

resize_array

cycles elements instead of favouring earlier ones

make_even

similar earlier-favouring behaviour for balancing 2 iterables

Examples

>>> resize_preserving_order(np.array([]), 5)
array([0., 0., 0., 0., 0.])

>>> nparray = np.array([[1, 2], [3, 4]])
>>> resize_preserving_order(nparray, 1)
array([[1, 2]])

>>> resize_preserving_order(nparray, 3)
array([[1, 2],
       [1, 2],
       [3, 4]])
Parameters:
  • nparray (npt.NDArray[np.float64])

  • length (int)

Return type:

npt.NDArray[np.float64]

resize_with_interpolation(nparray, length)[source]
Extends/truncates nparray so that len(result) == length.

New elements are interpolated to achieve the desired length.

Note that if nparray’s length changes, its dtype may too (e.g. int -> float: see Examples)

See also

resize_array

cycles elements instead of interpolating

resize_preserving_order

favours earlier elements instead of interpolating

Examples

>>> nparray = np.array([[1, 2], [3, 4]])
>>> resize_with_interpolation(nparray, 1)
array([[1., 2.]])
>>> resize_with_interpolation(nparray, 4)
array([[1.        , 2.        ],
       [1.66666667, 2.66666667],
       [2.33333333, 3.33333333],
       [3.        , 4.        ]])
>>> nparray = np.array([[[1, 2], [3, 4]]])
>>> nparray = np.array([[1, 2], [3, 4], [5, 6]])
>>> resize_with_interpolation(nparray, 4)
array([[1.        , 2.        ],
       [2.33333333, 3.33333333],
       [3.66666667, 4.66666667],
       [5.        , 6.        ]])
>>> nparray = np.array([[1, 2], [3, 4], [1, 2]])
>>> resize_with_interpolation(nparray, 4)
array([[1.        , 2.        ],
       [2.33333333, 3.33333333],
       [2.33333333, 3.33333333],
       [1.        , 2.        ]])
Parameters:
  • nparray (npt.NDArray[F])

  • length (int)

Return type:

npt.NDArray[F]

stretch_array_to_length(nparray, length)[source]
Parameters:
  • nparray (npt.NDArray[F])

  • length (int)

Return type:

npt.NDArray[F]

tuplify(obj: str) tuple[str][source]
tuplify(obj: Iterable[T]) tuple[T]
tuplify(obj: T) tuple[T]

Converts obj to a tuple intelligently.

Examples

>>> tuplify("str")
('str',)
>>> tuplify([1, 2])
(1, 2)
>>> tuplify(len)
(<built-in function len>,)
Parameters:

obj (str | Iterable[T] | T)

Return type:

tuple[str] | tuple[T]

uniq_chain(*args)[source]
Returns a generator that yields all unique elements of the Iterables

provided via args in the order provided.

Examples

>>> gen = uniq_chain([1, 2], [2, 3], [1, 4, 4])
>>> from collections.abc import Generator
>>> isinstance(gen, Generator)
True
>>> tuple(gen)
(1, 2, 3, 4)
Parameters:

args (Iterable[T])

Return type:

Generator[T, None, None]