Get unique values from a list in python
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
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.
Related reading
- Get unique values from a list in python
- get vs. list in Kubernetes RBAC
- Getting a list of all subdirectories in the current directory
- Getting a list of values from a list of dicts
- Getting a Large List of Nouns or Adjectives in Python with NLTK; or Python Mad Libs
- Getting a list of files in a directory with a glob
- Getting a map to return a list in Python 3.x
- Getting a random value from a JavaScript array

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.