text: from typing import Callable, TypeVar
  
  G = TypeVar('G')
  K = TypeVar('K')
  V = TypeVar('V')
  
  
  def group_by(dictionary: dict[K, V], key: Callable[[K, V], G]) -> dict[G, dict[K, V]]:
      """
      Groups the elements in the iterable into a dict of lists according to key.
      Functions like Kotlin's groupBy().
      """
      groups: dict[G, dict[K, V]] = {}
      for k, v in dictionary.items():
          g = key(k, v)
          if g not in groups:
              groups[g] = {}
          groups[g][k] = v
      return groups
  