Skip to content

Instantly share code, notes, and snippets.

@epignatelli
Created February 5, 2026 14:36
Show Gist options
  • Select an option

  • Save epignatelli/a49192b2a2330f9c281ad8381dea9004 to your computer and use it in GitHub Desktop.

Select an option

Save epignatelli/a49192b2a2330f9c281ad8381dea9004 to your computer and use it in GitHub Desktop.
type_hinting.py
def group_average(scores):
"""Compute the average score for each group.
Args:
scores: Records like {"group": str, "score": number}. Records with missing
keys or invalid scores are ignored.
Returns:
Mapping from group name to average score.
"""
totals = {}
counts = {}
for row in scores:
group = row.get("group")
if not group:
continue
try:
score = float(row.get("score"))
except (TypeError, ValueError):
continue
totals[group] = totals.get(group, 0.0) + score
counts[group] = counts.get(group, 0) + 1
return {g: totals[g] / counts[g] for g in totals}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment