Dictionary - one key, many values
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

