Remove duplicate dict in 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 duplicate dictionaries from a Python list sounds simple until you define what "duplicate" means. Sometimes the whole dictionary must match, and sometimes only one key such as id matters. The right solution depends on that rule and on whether you want to preserve the first item, keep the last one, or just return unique records in any order.
Define Equality Before Writing Code
With plain dictionaries, Python cannot put them directly into a set because dictionaries are mutable and unhashable. That means you need a transformation step or a lookup structure.
There are three common duplicate rules:
- Full dictionary equality.
- Equality by one key such as
id. - Equality by a subset of keys such as
nameandemail.
Pick the rule first. Otherwise, you will write code that removes the wrong records.
Keep First Occurrence by Full Dictionary Value
If two dictionaries are duplicates only when every key and value matches, one practical approach is converting each item into a hashable representation.
This preserves input order and keeps the first copy of each unique dictionary.
Deduplicate by Key
In many real systems, records are unique by one field such as id. In that case, deduplication is simpler and more explicit.
This keeps the first item for each id. That behavior should be documented, because some applications need the last item instead.
Keep Last Occurrence by Key
If later records should override earlier ones, use a dictionary keyed by the dedupe field.
Because dictionaries preserve insertion order, the final values reflect the last record seen for each key.
Deduplicate by Multiple Fields
Sometimes one field is not enough. Build a tuple from the fields that define uniqueness.
This is usually clearer than trying to compare whole dictionaries when only a subset matters.
Performance and Memory Tradeoffs
All of these approaches are typically O(n) time because each item is processed once. They also use extra memory for the seen set or lookup dictionary.
That tradeoff is usually acceptable, but for very large datasets:
- Deduplicate during ingestion instead of after building a huge list.
- Stream data if possible.
- Choose the smallest marker needed for uniqueness.
A small tuple such as (id,) is cheaper than sorting every dictionary item for every row.
When Data Is Nested
If dictionaries contain nested lists or dictionaries, tuple(sorted(item.items())) may fail because nested structures are still unhashable. In those cases, either:
- Normalize records into a simpler key.
- Serialize consistently with
json.dumps(..., sort_keys=True). - Deduplicate by domain key instead of full nested structure.
For example:
This works, but key-based dedupe is often faster and easier to reason about.
Common Pitfalls
- Removing duplicates without first defining what counts as duplicate.
- Using full-dictionary equality when only
idshould matter. - Forgetting whether the code keeps first or last occurrence.
- Building markers from unordered fields inconsistently.
- Using expensive full serialization when a simple key would solve the problem.
Summary
- Deduplication strategy depends on how your application defines equality.
- Use a hashable marker and a
seenset to preserve first occurrence. - Use a dictionary keyed by
idor another field to preserve last occurrence. - Prefer key-based dedupe over full-dictionary comparison when domain rules allow it.
- Be careful with nested structures, because plain dictionaries are unhashable.
Related reading
- Remove duplicate objects from an array using javascript
- Remove element of a regular array
- Remove elements from collection while iterating
- remove elements from link list whose sum equals to zero
- Remove empty strings from a list of strings
- Remove empty strings from a list of strings
- Remove empty array elements
- Remove empty elements from an array in Javascript

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.