A list of snippets, three buckets — short, medium, long. You want a dict showing how many snippets fell into each. What's the cleanest way to count without writing three if/elif branches inside a loop?
Start with {"short": 0, "medium": 0, "long": 0} and bump whichever bucket fits? Or is there a smarter pattern?
The seed-and-bump pattern is the clearest. Seed a dict with all three keys at zero so the counter is always complete — then decide the bucket from the snippet length and bump:
buckets = {"short": 0, "medium": 0, "long": 0}
for s in snippets:
key = "short" if len(s) < 40 else "long" if len(s) >= 120 else "medium"
buckets[key] += 1So the chained conditional picks the bucket in one line, and the seed dict means I never get a KeyError?
Both wins at once. Pre-seeding the counter is the standard trick whenever your caller expects all keys present — even zero counts matter for rendering a clean report:
def snippets_by_length_bucket(max_results: int) -> dict:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
snippets = [m.get("snippet", "") for m in result.get("messages", [])]
buckets = {"short": 0, "medium": 0, "long": 0}
for s in snippets:
key = "short" if len(s) < 40 else "long" if len(s) >= 120 else "medium"
buckets[key] += 1
return bucketsWhat if my inbox is empty — the loop never runs, so the three zeros stay untouched?
That's the whole point of seeding. Zero emails means {"short": 0, "medium": 0, "long": 0} — shape preserved, counts honest. The caller renders the same report whether you processed zero snippets or a thousand.
So this pattern works for any bucketing problem — urgency, sender, day of week — swap the key function and you're done?
Seed, key, bump. Three lines, any categorical aggregation. You now have the core shape every morning-report summary will share from here on.
TL;DR: Seed the dict with every key at zero, then bump each item's bucket inside the loop.
{"short": 0, "medium": 0, "long": 0}buckets[key] += 1| Approach | Downside |
|---|---|
{} + .get(k, 0) | Missing keys drop from report |
defaultdict(int) | Unexpected keys appear silently |
| Pre-seeded dict | All expected keys always present |
Pre-seeding makes the contract explicit: every run of the function returns the same key set, even on an empty input.
A list of snippets, three buckets — short, medium, long. You want a dict showing how many snippets fell into each. What's the cleanest way to count without writing three if/elif branches inside a loop?
Start with {"short": 0, "medium": 0, "long": 0} and bump whichever bucket fits? Or is there a smarter pattern?
The seed-and-bump pattern is the clearest. Seed a dict with all three keys at zero so the counter is always complete — then decide the bucket from the snippet length and bump:
buckets = {"short": 0, "medium": 0, "long": 0}
for s in snippets:
key = "short" if len(s) < 40 else "long" if len(s) >= 120 else "medium"
buckets[key] += 1So the chained conditional picks the bucket in one line, and the seed dict means I never get a KeyError?
Both wins at once. Pre-seeding the counter is the standard trick whenever your caller expects all keys present — even zero counts matter for rendering a clean report:
def snippets_by_length_bucket(max_results: int) -> dict:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
snippets = [m.get("snippet", "") for m in result.get("messages", [])]
buckets = {"short": 0, "medium": 0, "long": 0}
for s in snippets:
key = "short" if len(s) < 40 else "long" if len(s) >= 120 else "medium"
buckets[key] += 1
return bucketsWhat if my inbox is empty — the loop never runs, so the three zeros stay untouched?
That's the whole point of seeding. Zero emails means {"short": 0, "medium": 0, "long": 0} — shape preserved, counts honest. The caller renders the same report whether you processed zero snippets or a thousand.
So this pattern works for any bucketing problem — urgency, sender, day of week — swap the key function and you're done?
Seed, key, bump. Three lines, any categorical aggregation. You now have the core shape every morning-report summary will share from here on.
TL;DR: Seed the dict with every key at zero, then bump each item's bucket inside the loop.
{"short": 0, "medium": 0, "long": 0}buckets[key] += 1| Approach | Downside |
|---|---|
{} + .get(k, 0) | Missing keys drop from report |
defaultdict(int) | Unexpected keys appear silently |
| Pre-seeded dict | All expected keys always present |
Pre-seeding makes the contract explicit: every run of the function returns the same key set, even on an empty input.
Create a free account to get started. Paid plans unlock all tracks.