epy.groupby

Contents

epy.groupby#

etils.epy.groupby(iterable: typing.Iterable[etils.epy.itertools._Tin], *, key: typing.Callable[[etils.epy.itertools._Tin], etils.epy.itertools._K], value: typing.Callable[[etils.epy.itertools._Tin], etils.epy.itertools._Tout] = <function _identity>) dict[etils.epy.itertools._K, list[etils.epy.itertools._Tout]][source]#

Similar to itertools.groupby but return result as a dict().

Example:

out = epy.groupby(
    ['555', '4', '11', '11', '333'],
    key=len,
    value=int,
)
# Order is consistent with above
assert out == {
    3: [555, 333],
    1: [4],
    2: [11, 11],
}

Other difference with itertools.groupby:

  • Iterable do not need to be sorted. Order of the original iterator is preserved in the group.

  • Transformation can be applied to the value too

Parameters:
  • iterable – The iterable to group

  • key – Mapping applied to group the values (should return a hashable)

  • value – Mapping applied to the values

Returns:

The dict