You can aggregate and filter. Now rank. Combine text from three APIs, sort by character length from longest to shortest, return the top N. What Python function does the sort?
sorted() with a key argument? But I always forget how reverse works.
sorted(items, key=len, reverse=True) gives you longest-first. key=len tells sort to compare by length, reverse=True flips ascending to descending:
ranked = sorted(combined, key=len, reverse=True)
top = ranked[:n]Slicing ranked[:n] handles over-indexing safely — asking for 100 from a 3-item list just returns all 3?
Exactly. Python slice semantics mean you never hit an IndexError. Here's the full function with three reads, one sort, one slice:
def rank_by_length(n: int, max_items: int) -> list:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_items})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
tasks = toolset.execute_action(Action.GOOGLETASKS_LIST_TASKS, {"max_results": max_items})
combined = (
[m.get("snippet", "") for m in emails.get("messages", [])]
+ [e.get("summary", "") for e in events.get("items", [])]
+ [t.get("title", "") for t in tasks.get("items", [])]
)
ranked = sorted(combined, key=len, reverse=True)
top = ranked[:n]
print(f"Top {len(top)} items by length from {len(combined)} total")
return topIs sorted() stable? If two items have the same length, is the original order preserved?
Yes. Python's sort is stable — equal keys preserve their relative order. That means items from Gmail keep their relative order among themselves even after the sort, and the same for events and tasks.
So one call gives me the longest pieces of text across my entire inbox, calendar, and task list — the stuff most likely to have real content?
Three live APIs, one sort, one slice. Top of the list is always the densest content. That closes Week 1 — next week you start shaping the richer structures this ranking feeds into.
TL;DR: sorted(items, key=len, reverse=True)[:n] gives you the top N by length across any list of strings — one line, stable, safe on small lists.
key=len — sort by length, not alphabeticallyreverse=True — longest first[:n] — safe even when n exceeds len(items); returns the whole list if fewer than n exist| Input | Output |
|---|---|
two items with same len | original order preserved |
two items with different len | longer one first |
n > len(items) | whole list, no error |
You can aggregate and filter. Now rank. Combine text from three APIs, sort by character length from longest to shortest, return the top N. What Python function does the sort?
sorted() with a key argument? But I always forget how reverse works.
sorted(items, key=len, reverse=True) gives you longest-first. key=len tells sort to compare by length, reverse=True flips ascending to descending:
ranked = sorted(combined, key=len, reverse=True)
top = ranked[:n]Slicing ranked[:n] handles over-indexing safely — asking for 100 from a 3-item list just returns all 3?
Exactly. Python slice semantics mean you never hit an IndexError. Here's the full function with three reads, one sort, one slice:
def rank_by_length(n: int, max_items: int) -> list:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_items})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
tasks = toolset.execute_action(Action.GOOGLETASKS_LIST_TASKS, {"max_results": max_items})
combined = (
[m.get("snippet", "") for m in emails.get("messages", [])]
+ [e.get("summary", "") for e in events.get("items", [])]
+ [t.get("title", "") for t in tasks.get("items", [])]
)
ranked = sorted(combined, key=len, reverse=True)
top = ranked[:n]
print(f"Top {len(top)} items by length from {len(combined)} total")
return topIs sorted() stable? If two items have the same length, is the original order preserved?
Yes. Python's sort is stable — equal keys preserve their relative order. That means items from Gmail keep their relative order among themselves even after the sort, and the same for events and tasks.
So one call gives me the longest pieces of text across my entire inbox, calendar, and task list — the stuff most likely to have real content?
Three live APIs, one sort, one slice. Top of the list is always the densest content. That closes Week 1 — next week you start shaping the richer structures this ranking feeds into.
TL;DR: sorted(items, key=len, reverse=True)[:n] gives you the top N by length across any list of strings — one line, stable, safe on small lists.
key=len — sort by length, not alphabeticallyreverse=True — longest first[:n] — safe even when n exceeds len(items); returns the whole list if fewer than n exist| Input | Output |
|---|---|
two items with same len | original order preserved |
two items with different len | longer one first |
n > len(items) | whole list, no error |
Create a free account to get started. Paid plans unlock all tracks.