import collections


def _group_by(input_list, key_fn):
  """Group a list according to a key function (with a hashable range)."""
  result = collections.defaultdict(list)
  for x in input_list:
    result[key_fn(x)].append(x)
  return result