Removing duplicates in lists
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 list sounds easy until you care about order, data type, or performance. The best technique depends on whether the elements are hashable, whether the original order matters, and whether you want to keep the first duplicate or the last one.
In Python, the most common answers are a set, dict.fromkeys, or an explicit loop with a seen set. Each one is correct for a different version of the problem.
Use set When Order Does Not Matter
If the elements are hashable and the output order is irrelevant, the shortest answer is:
This is fast and simple, but it does not preserve the original order. If the list order has meaning, this approach is too destructive.
Preserve First-Seen Order With dict.fromkeys
In modern Python, dictionaries preserve insertion order, so dict.fromkeys is a clean order-preserving solution for hashable items:
This keeps the first occurrence of each value and removes later duplicates. For many everyday Python tasks, this is the best default.
Use an Explicit Loop for More Control
If you want custom logic while deduplicating, use a seen set plus a loop:
This is especially useful when you want to log skipped values, normalize items, or apply additional rules during the pass.
Structured Data Often Needs an Explicit Key
If the list contains dictionaries or objects, "duplicate" usually means duplicate by some key, not by full object identity:
This keeps the first record for each id. If your business rule should keep the last occurrence instead, use a dictionary keyed by id and let later entries overwrite earlier ones.
Unhashable Items Need a Different Strategy
Lists of lists cannot go directly into a set because lists are unhashable:
This is acceptable for small inputs. If possible, convert to a hashable representation first:
That keeps order while allowing hash-based deduplication.
Common Pitfalls
The biggest mistake is using set and then being surprised when the original order is lost. If order matters, use an order-preserving approach.
Another common issue is assuming all elements are hashable. Lists, dictionaries, and many custom objects cannot go straight into a set.
Developers also sometimes forget to define which duplicate should win. Keeping the first and keeping the last are different business rules.
Finally, be careful with large unhashable collections. A repeated item not in unique check can become slow, so explicit key extraction is often the better path.
Summary
- Use
setwhen order does not matter and the items are hashable. - Use
dict.fromkeyswhen you want to preserve first-seen order. - Use an explicit loop with a
seenset when you need custom control. - For structured records, deduplicate by an explicit key rather than by raw object identity.
- Decide whether the first or last duplicate should win before choosing the method.
Related reading
- Removing duplicates in lists
- Removing duplicates in lists
- Removing multiple keys from a dictionary safely
- Removing object from array in Swift 3
- Removing viewcontrollers from navigation stack
- Rename a dictionary key
- Reorder vector using a vector of indices
- Reordering a list to maximize difference of adjacent elements

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.