Get unique values from a list in python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Removing duplicates from a Python list is a common preprocessing step for reporting, validation, and feature pipelines. The best method depends on whether order matters, whether values are hashable, and how large the data is. This guide compares practical options and highlights their tradeoffs.
Fastest Simple Case: Hashable Items, Order Not Required
When order is irrelevant and items are hashable, convert to set.
Pros:
- Usually fastest in pure Python.
- Minimal code.
Cons:
- Output order is arbitrary.
Use this for internal math operations where relative order has no meaning.
Preserve Original Order
Most application code needs first-seen order. dict.fromkeys is concise and fast in modern Python.
Why it works:
- Dictionary keys are unique.
- Insertion order is preserved in current Python implementations.
Equivalent explicit pattern with seen set is also useful when you need custom side effects.
First Unique by Key
Sometimes elements are complex objects and uniqueness should be based on one field.
This keeps first occurrence for each key and is common in API dedup workflows.
Unhashable Items
Lists and dictionaries are unhashable, so set-based methods fail directly.
Example failure:
Solutions:
- Convert each element to hashable representation such as tuple.
- Serialize deterministic key for complex structures.
For nested dictionaries, a normalized JSON key can work if key ordering is controlled.
Scaling Considerations
For very large inputs:
- Hash-based approaches are
O(n)average time. - Memory usage grows with number of unique elements.
If data exceeds memory, process in chunks and persist seen keys in an external store or probabilistic structure.
Streaming pattern:
This avoids creating a second large output list immediately.
Benchmarks and Readability
Micro-optimization is rarely needed unless lists are huge. In many codebases, readability wins:
dict.fromkeysfor order-preserving simple types.setconversion for order-agnostic tasks.- explicit helper for custom uniqueness keys.
Benchmark only when dedup is in a measured hot path.
Common Pitfalls
- Using
setand expecting original order. Fix by usingdict.fromkeysor seen-set pattern. - Applying set to unhashable items. Fix by converting items to hashable keys.
- Deduplicating dictionaries by full object identity when one field defines uniqueness. Fix by using key-based helper.
- Assuming dedup always improves performance. Fix by measuring end-to-end impact, including memory overhead.
- Repeating dedup logic in many files. Fix by centralizing tested utility functions.
Summary
- Use
setfor quick unique extraction when order does not matter. - Use
dict.fromkeysfor order-preserving dedup of hashable items. - Use key-based helpers for complex object collections.
- Handle unhashable items by converting to stable hashable keys.
- Favor clear utility functions and benchmark only for true hot paths.

