Dictionary - one key, many values
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
In Python, a dictionary maps one key to one value, but that value can itself be a collection. So when people ask for "one key, many values", the normal solution is a dictionary whose values are lists, sets, or another container type.
That pattern is common for grouping records, building indexes, and collecting repeated events under the same label. The main design choice is not whether Python allows it, but which collection type best matches the behavior you want.
Using Lists as the Value
If you want to preserve insertion order and allow duplicates, store a list for each key.
This is the simplest solution when repeated values are meaningful.
Building the Dictionary Incrementally
When the keys are not known in advance, defaultdict(list) is a clean option.
defaultdict creates the list automatically the first time a key is used, so you do not need an explicit existence check.
setdefault as a Built-In Alternative
If you do not want defaultdict, use setdefault.
This is convenient for small scripts, though defaultdict is often cleaner for repeated grouping logic.
Use a Set When Duplicates Should Be Ignored
If each key should contain unique values only, use a set instead of a list.
The second "python" is ignored automatically because sets keep unique values.
Choosing the Right Collection
Use a list when:
- Order matters
- Duplicate values are meaningful
- You may need indexing or stable output order
Use a set when:
- Uniqueness matters more than order
- Membership checks should be fast
- Duplicate inserts should collapse automatically
You can also use tuples, counters, nested dictionaries, or custom objects if the grouped value has more structure than a simple collection.
A Real Grouping Example
Grouping rows by key is one of the most common uses of this pattern.
Here, one dictionary key collects many numeric values, and a later step reduces them into totals.
Common Pitfalls
The classic mistake is reusing the same mutable list for multiple keys by accident. Each key should get its own list or set.
Another issue is choosing a list when the real requirement is uniqueness. That leads to manual duplicate checks that a set would have handled automatically.
Be careful when exposing the stored collections to other parts of the program. Lists and sets are mutable, so callers can modify them unless you copy or wrap the values.
Finally, remember that dictionary keys are still unique. "One key, many values" means one key maps to one collection object, not that the dictionary itself stores duplicate keys.
Summary
- Python handles "one key, many values" by storing a collection as the dictionary value.
- Use lists when order and duplicates matter.
- Use sets when uniqueness matters.
- '
defaultdictandsetdefaultare the standard tools for building these mappings incrementally.' - Pick the value type based on how the grouped data will be queried and updated.
Related reading
- Dictionary using Red-Black tree - deletion error
- Dictionary.FirstOrDefault how to determine if a result was found
- Diff for Directed Acyclic Graphs
- Difference and advantages between dijkstra A star
- Dictionary serialization for DynamoDB ExclusiveStartKey not working
- Difference between __getattr__ and __getattribute__
- Difference between AVL trees and splay trees
- Difference between bidirectional_dynamic_rnn and stack_bidirectional_dynamic_rnn in Tensorflow

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.