Technical skill practice

Python collections: Count Labels and Keep a Bounded Recent Trail

Python’s collections module supplies specialized containers alongside the built-in list and dict. Counter can summarize item frequencies, while deque can represent a recent trail or queue. Choose the container by the operation you need rather than importing every container just because the module is called collections.

Start with a small, explicit task

Build a compact fictional activity summary with two separate requirements: count repeated color labels and retain only the last three activity names. The frequency table and the bounded trail serve different purposes, so they use different containers and independent fixtures.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_collections.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

[('blue', 3), ('amber', 2)]

review ['edit', 'save']

from collections import Counter, deque
labels = ["blue", "amber", "blue", "green", "amber", "blue"]
counts = Counter(labels)
assert counts["blue"] == 3 and counts["absent"] == 0
assert counts.most_common(2) == [("blue", 3), ("amber", 2)]
trail = deque(maxlen=3)
for action in ["screen", "review", "edit", "save"]:
    trail.append(action)
assert list(trail) == ["review", "edit", "save"]
first_retained = trail.popleft()
assert first_retained == "review"
assert list(trail) == ["edit", "save"]
print(counts.most_common(2))
print(first_retained, list(trail))

Explain the result

Counter produces a frequency view without calling list.count separately for each label. blue occurs three times and amber twice; the absent label returns zero when accessed in this Counter. That is a specific counting-container behavior, not the missing-key rule of every ordinary dictionary.

The deque has a maximum length of three, so appending save after three earlier actions discards screen from the left. popleft then returns review, the oldest retained action, leaving edit and save. This is intentional information loss under the recent-trail requirement. Use an unbounded or separately persisted record when older events must be retained.

Avoid these mistakes

list and dict are built in and do not need to be imported from collections. A bounded deque is not an audit log because it discards older entries. This small example checks semantics rather than benchmarking container speed, and it does not provide concurrent event processing.

Questions, answered

Does Counter replace every dictionary?

No. Use it when counting is the task; ordinary mappings may need different missing-key semantics.

What happens when a bounded deque fills?

In this append-to-right example, adding a new item discards the oldest leftmost item.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.