This commit is contained in:
Christian Mantha
2026-03-02 19:10:52 -05:00
commit 2ca0b9ef7c
28907 changed files with 5233713 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
from collections import defaultdict
from typing import Callable, Collection, Dict, List, TypeVar
__all__ = ["group_by"]
K = TypeVar("K")
T = TypeVar("T")
def group_by(items: Collection[T], key_fn: Callable[[T], K]) -> Dict[K, List[T]]:
"""Group an unsorted collection of items by a key derived via a function."""
result: Dict[K, List[T]] = defaultdict(list)
for item in items:
key = key_fn(item)
result[key].append(item)
return result