python 24 lines · 5 steps

Finding the top-N items in a stream

Count elements as they arrive, then use a heap to pull out the N most frequent without sorting everything.

Explained by highlit
1import heapq
2from collections import Counter
3from typing import Iterable, Hashable
4 
5 
6def top_n_from_stream(stream: Iterable[Hashable], n: int) -> list[tuple[Hashable, int]]:
7 counts: Counter[Hashable] = Counter()
8 for item in stream:
9 counts[item] += 1
10 
11 return heapq.nlargest(n, counts.items(), key=lambda pair: pair[1])
12 
13 
14def top_n_words(paths: Iterable[str], n: int = 10) -> list[tuple[str, int]]:
15 def token_stream() -> Iterable[str]:
16 for path in paths:
17 with open(path, encoding="utf-8") as fh:
18 for line in fh:
19 for token in line.split():
20 cleaned = token.strip(".,!?;:\"'()[]").lower()
21 if cleaned:
22 yield cleaned
23 
24 return top_n_from_stream(token_stream(), n)
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1heapq.nlargest finds the top N in O(m log n) without fully sorting the collection.
  2. 2Generators let you process arbitrarily large input with constant memory over the token stream.
  3. 3Separating counting from top-N selection keeps each function small and reusable.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Finding the top-N items in a stream — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code