aspis.common.add_index

aspis.common.add_index(func, arr)[source]

Maps a function over a sequence, passing the index as a second argument, allowing the map function to have access to the index of the element being processed.

Parameters:
  • func (Callable[[T, int], U]) – A function that takes an element and its index as arguments and returns a transformed result.

  • arr (Sequence[T]) – The sequence to be mapped over.

Returns:

A new sequence with the results of the function applied to each element.

Return type:

Sequence[U]

Example

>>> add_index(lambda x, idx: f"{idx}: {x}", ["a", "b", "c"])
['0: a', '1: b', '2: c']