Using a dictionary to count the items in a list
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Counting how often each value appears in a list is a core operation in analytics, data cleaning, and event processing. In Python, dictionaries provide an efficient one-pass solution with clear semantics. The best pattern depends on whether you need quick totals, normalized grouping, streaming updates, or top-frequency reporting.
Basic Dictionary Counting Pattern
The manual dictionary approach is simple and flexible.
This runs in linear time for typical hashable values and is easy to extend with custom logic before incrementing.
Use Counter for Concise Code
When you only need standard frequency counting, collections.Counter is shorter and expressive.
Counter is a dictionary subclass, so it remains compatible with many dictionary workflows.
Normalize Input Before Counting
Real data often includes case and whitespace variation. If business logic treats those values as equivalent, normalize first.
Skipping normalization leads to fragmented counts and misleading reports.
Counting Complex Objects with Derived Keys
List elements are not always plain strings. For dictionaries or objects, define a stable key based on your uniqueness rule.
Choosing the right key is critical. In some domains only one field should define identity.
Streaming Updates for Event Pipelines
When data arrives continuously, update counts incrementally instead of loading full datasets in memory.
This pattern fits background workers and log consumers where data volume is unbounded.
Ranking Results by Frequency
Dictionary insertion order is not frequency order. Sort explicitly when output needs ranking.
For very large maps and top-N use cases, heapq.nlargest can be more efficient than sorting everything.
Memory and Performance Considerations
Counting is usually O(n) time and O(k) memory where k is unique key count. High-cardinality keys can consume significant memory.
If cardinality is huge, consider:
- Partitioned counting by key range.
- Periodic flushing to external storage.
- Approximate counting techniques when exact counts are not mandatory.
For most application workloads, regular dictionaries or Counter remain the practical default.
Testing Frequency Logic
Unit tests should include:
- Empty list input.
- Mixed case and whitespace examples.
- Expected frequency ordering after sorting.
- Non-string keys if your pipeline allows them.
Small tests catch many regressions after key-normalization changes.
Common Pitfalls
- Forgetting default initialization and triggering key errors.
- Counting raw values when business rules require normalization.
- Using nested loops and creating avoidable quadratic performance.
- Assuming dictionary iteration order reflects highest frequency.
- Defining uniqueness keys inconsistently across code paths.
Summary
- Dictionary counting is an efficient one-pass way to compute frequencies.
- '
dict.getis flexible, whileCounteris concise for standard cases.' - Normalize inputs when semantic equality differs from literal equality.
- Use derived keys for complex records.
- Sort explicitly when consumers need ranked frequency output.
Related reading
- Using a global dictionary with threads in Python
- Using a QuadTree to get all points within a bounding circle
- using a tf.Tensor as a Python bool is not allowed in Graph execution. Use Eager execution or decorate this function with tf.function
- Using BFS for topological sort
- Using a variable for num_splits for tf.split
- Using async in non-async method
- Using BFS for Weighted Graphs
- Using Dijkstra's algorithm to find a path that can carry the most weight

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.